C# Scheduled Series Of Tasks
I am in the process of converting a Classic ASP/VBScript application to C#/ASP.Net. The VBScript part of the application is a series of individual scripts performed each month on a specific date with each individual task set up in Windows Scheduler. There are about 35 tasks which include database inserts (saving monthly historical data, saving summary billing data) as well as exporting a series of Crystal Reports as PDFs and Excel. Each task is scheduled about a minute apart, as I do not want to overwhelm the database server with simultaneous requests.
As I approach this in C#, I wonder if there is a way to this in one application rather than the separate script approach I took with VBScript. I am comfortable with C# as I have been using it to convert the website part of the application to ASP.Net MVC over the past year or so, but I have no experience with timed events in C#. I was thinking of approaching this by making a separate function for each task, with sleep between each task to allow enough time for the database processing to occur before moving on to the next task. Something like the following:
p.DoTask1();
Thread.Sleep(60000);
p.DoTask2();
Thread.Sleep(60000);
p.DoTask3();
Thread.Sleep(60000);
etc ...
This would be wrapped up in a console app which itself would be set up in Windows Scheduler.
Is this a sound approach? Are there better approaches? I have read about timers and tasks, but I am not sure if they are appropriate for what I am trying to accomplish.
You could fire each task after the previous has finished using Task.ContinueWith. See this related answer for more detail.
DB queries return rows affected which is how you would tell the database had finished running the query you executed, rather than just having passed it off to the database.
The advantages I see with using a method like this over simply sleeping the threads are that you'd never have multiple tasks running at once and there would be no downtime between tasks.
How crucial that is depends on your situation which you have greater insight into.
The console app being fired up by Windows Scheduler is fine to me. I do this myself for a database integration program I did that runs once a day. For timed events, I use System.Threading.Timer class, like this:
//here you set a time to start
var timeToStart = 60000;
//here an interval, if it's not recurring use Timeout.Infinite
var tRefresh = Timeout.Infinite;
//here the method, I make it static
TimerCallback tcb1 = ClassName.MethodName;
var timer = new System.Threading.Timer(tcb1, null, timeToStart, tRefresh);
You can create several timers, and assign them the different tasks, with different start times to solve your problem.
Just another suggestion for running this for what it's worth. You could take a look at Quartz.Net rather than running a console app under windows, that way you can run the tasks (or Jobs in Quartz) via your website and deploy and update all your code at once rather than needing a separate console app. You can schedule them using CRON timing settings - although the site needs to be running so you may need monitoring or a ping setup if running at odd times.
You could then setup your tasks with separate schedules, or as others have suggested above, have one task with steps that run when others complete.
链接地址: http://www.djcxy.com/p/38892.html上一篇: 计划任务在使用https时不适用于ColdFusion 11
下一篇: C#计划任务系列