开发者

I want to frequently check the server for new data.Using C# TImers

I want to f开发者_运维知识库requently check the server for new data. means i want to fire the select command for every 20 minutes to check database table. i am using windows form application 2008.


You could use a Timer. And the corresponding documentation on MSDN:

var timer = new Timer(TimeSpan.FromMinutes(20).TotalMilliseconds);
timer.Elapsed += (sender, e) =>
{
    // TODO: send a query to the database
};
timer.Start();

In Windows Forms you could also use the System.Windows.Forms.Timer class.


If the app does nothing else I recommend scheduling it. Scheduling the app frees you from a whole set of problems ranging from not having to implement Service-behaviour to avoiding instability due to not freeing unmanaged resources over time. Apps running for a long time can encounter problems, crash, freeze, etc. The task scheduler has options to kill long running apps and similar, so it is a good friend for such tasks.

If the app modifies its GUI use System.Forms.Timer (drag out a timer GUI control to the form).

If the app runs in the background use either System.Threading.Timer or a dedicated System.Threading.Thread with a wait loop containing System.Threading.Thread.Sleep.

If your app runs from ASP.Net then rumor has it Quartz is the way to go. Personally I'd recommend you don't run schedules from ASP.Net and instead take a good look at your architecture.


Quartz.NET is a very good library. I'm using it in my windows services. I suggest you should use cron trigger.

http://quartznet.sourceforge.net/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜