Nirmal
Nirmal

Reputation:

How can I write a scheduler application in .NET?

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

Upvotes: 2

Views: 9423

Answers (6)

abatishchev
abatishchev

Reputation: 100366

You can try use Windows Task Scheduler API

Upvotes: 0

Mark Allen
Mark Allen

Reputation: 1205

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.

Upvotes: 0

user22467
user22467

Reputation:

You could also try Quartz.Net.

Upvotes: 4

Chris Wenham
Chris Wenham

Reputation: 24037

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.

Upvotes: 0

Ian Jacobs
Ian Jacobs

Reputation: 5501

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.

Upvotes: -1

Sklivvz
Sklivvz

Reputation: 31173

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.

Upvotes: 0

Related Questions