Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top