views:

290

answers:

3

Hi, I have to do 3 async operations parallely inside a Windows NT Service (using .Net2.0/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.

+1  A: 

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

sashaeve
All the 3 operations are also long time operations. Involves lot of processing. One of them is copying large MB files to a folder location. So, should I still resort to normal threads. I am not very clear and confident.
DotNetGuy
If you don't need alerts regarding progress changed and task completed - you may use threads otherwise use background worker.
sashaeve
+1  A: 

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.

Dave
Is it wrong to use thread pool threads.. ?
DotNetGuy
I think it depends. I use them all of the time right now, and haven't even used normal threads. But you need to be aware of the limit on thread pool threads. BackgroundWorker, Windows Workflow, and I think just using BeginInvoke on delegates uses the thread pool. I bet it's pretty easy to run out in a complicated app.
Dave
Cool ! In future then if I need to update some kind of UI for depicting the progress of anyof the 3 operations, then BckgrndWrkr will come in handy. And, do you foresee any issues in calling RunWorkerAsync() again in RunWorkerCompleted event ?
DotNetGuy
One more view here - http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22509585.html
DotNetGuy
The expert sexchange article has no very interesting content, as far as I can see. (To check yourself, you have to copy the link and paste it into google or similar, so that you come to the site from a search engine. Then just scroll to the bottom).
Joel in Gö
@Joel I almost thought this was a spambot post given your typo at the beginning of your comment. Or maybe it wasn't a typo. :)
Dave
A: 

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)

Barfieldmv