Search code examples
c#backgroundworkermemory-visibility

Does BackgroundWorker guarantee that memory changes made on the background thread become visible to the main thread?


If I use a BackgroundWorker to modify data structures in my application, is there a guarantee that changes made on the background thread will be visible to the main (UI) thread when the BackgroundWorker completes (e.g. within the RunWorkerCompleted event handler)? For bonus points: if so, what is the mechanism that guarantees this?


Solution

  • No, there is no such guarantee. You'll need to synchronize access to any shared memory yourself if you want to access it from multiple threads.

    Of course if you use the BGW built in mechanisms for passing data between threads, such as through the Result or Progress data that it stores, then it will properly synchronize access to that data.