开发者

BackgroundWorker Thread - C#

I have to do 3 async operations parallely inside a Windows NT Service (using .Net2.0/C#)开发者_如何学C. For that I am using Backgroundworker component.

  1. Is it a good option/approach?
  2. For continuous operation, I am calling RunWorkerAsync() again in RunWorkerCompleted event.

Please suggest me.


Usually BackgroundWorker is used for long-time operations. If you just need to execute 3 tasks in parallel you can use simple threads.


Usually, BackgroundWorker is used when you need RunWorkerCompleted to perform updates in a GUI. At least that's how I've been using it. It sounds like you want this to run constantly, so why not use a regular worker thread?

The biggest issue I see is that BackgroundWorker uses the .NET thread pool, so if you really want this to run continuously, you've just guaranteed to use up one of the thread pool threads, of which there are a limited number available.


I wouldn't be using backgroundworkers for the job you're describing here.

The background worker is meant to be used for some 'long' running operations while keeping a responsive UI. Using services breaks the UI pattern.

For a service I would be using a timer and three threads.

Let the timer check the existence of the three threads and restart them or report errors when needed. The three threads can do their own job in a while loop (don't forget to add a sleep(0) in there)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜