Is there a way to send data to BackgroundWorker after it has started?

I know that you can pass arguments through a call to the RunWorkerAsync function when the backgroundworker is first started, but can you pass it after it has already started? Or do I need to create my own concurrency form to handle the transfer of data from it from another thread?

+2


source to share


3 answers


You will need to add some sync and have a place where the background worker can read the data.



You cannot (easily) transfer the data to a second worker. It is much easier to just have a place where the worker can look for data and you can just add data to process. Just make sure you set up sync at this point, since (at least) two threads will be accessing the data at the same time.

+3


source


The BackgroundWorker API does not support a mechanism for passing additional data after a task has started.

However, the working procedure just runs on a different thread. You can pass data to this stream in the same way as you can pass data between two arbitrary streams. A few quick examples of how ...



  • Changing the state of static variables (possibly evil)
  • Changing the state of an object originally committed to a work routine (still a little wicked if not controlled properly)

You should carefully consider the effect of synchronizing these approaches.

+2


source


I prefer to use a static Queue, which is a background thread that periodically checks for new messages. This allows the background thread to run at its own pace. You can use callback methods to return to the main thread. And as Reed said, use synchronization like a static object to lock.

0


source







All Articles