Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 745
Louise Eggleton Avatar asked Oct 20 '22 10:10

Louise Eggleton


2 Answers

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.

like image 154
Amicable Avatar answered Oct 23 '22 10:10

Amicable


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.

like image 27
LeshracEvil Avatar answered Oct 23 '22 11:10

LeshracEvil