communicate with the BackgroundWorker

  • Thread starter Thread starter BenMoshe
  • Start date Start date
B

BenMoshe

I use BackgroundWorker class and wonder what are the alternatives for the
main thread to communicate with the BackgroundWorker thread?
Regards
BenMoshe
 
BenMoshe said:
I use BackgroundWorker class and wonder what are the alternatives for the
main thread to communicate with the BackgroundWorker thread?
Regards
BenMoshe

I never knew about the "BackgroundWorker" class till you asked about it but
it does seem to be a neat way of getting work done in a background thread in
Forms applications. It caters to the typical scenario of a background task
needing to send periodic status updates to the UI thread. As for
alternatives, you could always do what this class does internally and post
window messages from the background thread using platform invoke
(SendMessage, PostMessage etc.). But then why would you want to?
"BackgroundWorker" seems to make it so much more cleaner.
 
It depends on what the nature of the "communicate with" is. Is it one way
or two way? Do you really need to "communicate" or just have the form read
some progress of the thread?
 
BenMoshe skrev:
I use BackgroundWorker class and wonder what are the alternatives for the
main thread to communicate with the BackgroundWorker thread?
Regards
BenMoshe
Depends on how your backgroundworker are going to work.

If it is a typically producer-consumer type of work where the background
worker get instructions from the main program of what to do, perform the
process, and then wait for new instructions you can use a typically
Queue for the workload and with Monitor.Wait()/Pulse() communication.
 
Back
Top