我创建了一个窗口服务,即每60秒钟在管道中检查某个桌子。 对于增加的每个新行,我需要在服务器上进行一些重处理,有时可能需要60秒。
I created a Timer object in my service, that ticks every 60 seconds and invokes the wanted method.
Since I don t want this timer to tick while processing the new lines found, I wrapped the method in a lock { }
block, so this won t be accessible by another thread.
它看着这样的情况:
Timer serviceTimer = new Timer();
serviceTimer.Interval = 60;
serviceTimer.Elapsed += new ElapsedEventHandler(serviceTimer_Elapsed);
serviceTimer.Start();
void serviceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (this)
{
// do some heavy processing...
}
}
Now, I m wondering -
If my timer ticks, and finds a lot of new rows on the db, and now the processing will take more than 60 seconds, the next tick won t do any processing till the previous one finished. This is the effect I want.
但是,现在,这项服务将是 螺旋-Elapsed方法一旦完成第一次处理,即立即失效,或者将等待时间。
我想要的是——如果处理需要60秒以上的时间,就会把read锁锁起来,再等待60秒钟,以便我永远不会陷入等待上台的人到底的境地。
How can i accomplish this result ?
What is the best practice for doing this ?
感谢!