tags:

views:

1080

answers:

9

How can I write a scheduler application in C# .NET?

+2  A: 

A windows service application could do the job. Could you be more specific on what the application should do and what actually the tasks are, that need to be executed?

Matthias
+1  A: 

It all depends on your requirements:

  • If you have access to a database you use a table as a queue and a service to poll the queue at regular intervals.

  • If your application is client only (CLI) you can use the system scheduler ("Scheduled Tasks").

  • Lastly, if your application is only in a database (using the CLR in SQL Server 2005 for example) then you can create a SQL Server job to schedule it.

Sklivvz
A: 

You can also use the timer control to have the program fire of whatever event you want every X ticks, or even just one. The best solution really depends on what you're tring to accomplish though.

Ian Jacobs
A: 

Ayende wrote about scheduling options/frameworks here.

Mauricio Scheffer
A: 

Check out this discussion

Omar Kooheji
+1  A: 

Assuming you're writing some system that needs to perform an action at a specific clock time, the following would cover the fundamental task of raising an event.

Create a System.Timer for each event to be scheduled (wrap in an object that contains the parameters for the event). Set the timer by calculating the milliseconds until the event is supposed to happen. EG:

// Set event to occur on October 1st, 2008 at 12:30pm.
DateTime eventStarts = new DateTime(2008,10,1,12,30,00);
Timer timer = new Timer((eventStarts - DateTime.Now).TotalMilliseconds);

Since you didn't go into detail, the rest would be up to you; handle the timer.Elapsed event to do what you want, and write the application as a Windows Service or standalone or whatever.

C. Lawrence Wenham
+2  A: 

You could also try Quartz.Net.

Nick Aceves
A: 

Write a windows service, there are excellent help topics on MSDN about what you need to do in order to make it installable etc.

Next, add a timer to your project. Not a Winforms timer, those don't work in Windows Services. You'll notice this when the events don't fire. Figure out what your required timer resolution is - in other words, if you have something scheduled to start at midnight, is it Ok if it starts sometime between Midnight and 12:15AM? In production you'll set your timer to fire every X minutes, where X is whatever you can afford.

Finally, when I do this I use a Switch statement and an enum to make a state machine, which has states like "Starting", "Fatal Error", "Timer Elapsed / scan for work to do", and "Working". (I divide the above X by two, since it takes two Xs to actually do work.)

That may not be the best way of doing it, but I've done it that way a couple of times now and it has worked for me.

Mark Allen
A: 

You can try use Windows Task Scheduler API

abatishchev