开发者

Threading timer with worker threads?

is it possible to use threading timer to fire worker threads on timer event?

I want use threading.Timer because I am intending to using a windows service triggered by time, but all the example code I see using timer are usin开发者_如何学运维g callback/threadpool instead. But in my service, the task will be longer than a few seconds (up to a minutes) and involve rather complex logic rather than just update a few fields. I think that would justify my choice of workerthread rather than threadpool?


I had a similar situation where I needed a collection of threading timers. If my window service was stopped I needed any ongoing timer callbacks to complete.

You can use a WaitHandle when you dispose your timer, allowing any ongoing callbacks to complete:

var onWait = new ManualResetEvent(false);
timer.Dispose(onWait);
....
onWait.WaitOne(); 

Here is my related question I haven't run into any issues..yet. My callbacks do some heavy processing that would take up to 10-20 minutes to complete.


you could use the call back and the task parallel library to fire off your task.... Task Parallelism

However I believe this sample code is doing what you want... I know it uses a threadpool...

"it executes in a separate thread pool thread that is provided by the system."

... but why is that bad???

Also the task parallel library basically does a similar thing...

"Behind the scenes, tasks are queued to the ThreadPool, which has been enhanced with algorithms (like hill-climbing) that determine and adjust to the number of threads that maximizes throughput. "

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜