views:

595

answers:

4

I'm making a simple scheduler with C# in .Net. All it does is execute a simple select statement on a table in a SQL Server DB once per minute (this does not need to scale or anything... the db does not have a high load). Here is my proposed implementation:

static void Main(string[] args)
{
    while (true)
    {
        System.Threading.Thread.Sleep(timeout); // timeout is, say, 60000
        CheckTable();
    }
}

Is this ok? What is a better way?

p.s. Someone suggested using the Windows Forms Timer class... however that seems like overkill.

Cheers!

+4  A: 

While it is technically legal you are probably better of using a timer. They are not much more code to set up and you can let the runtime take care of spawning new threads. If you ever needed to use this again in another program it would also create a performance bottleneck where a timer would not.

The timer will add more code though since you need to use a timer trigger event.

Doug Trojan
That really depends on whether or not you want multiple threads running the same proc at the same time.
Jonathan Allen
+1  A: 

This question is related: http://stackoverflow.com/questions/169332

spoon16
+1  A: 

Close.

  1. This will run every (1 minute + time to call proc). Maybe that's OK, maybe it isn't. If it isn't OK you need to subtract the amount of time it took to ran.
  2. You should have a try-catch block around it. You don't want it to die entirely just because of a temporary database or network issue.
Jonathan Allen
+1  A: 

Quartz.net is a good solution for timing needs. It's pretty easy to setup (good tutorial on the site), and gives you a lot more flexibility than Timers. The CronTrigger is really powerful, and easy to configure.

Andy White