I have read the answers from How to wait for a BackgroundWorker to cancel?, but I couldn't find a solution to my specific problem:
My app has to load a large amount of data, but in most cases this data won't be required immediately after the app has started.
To minimize the delay when the user actually requests the data, I load it using a BackgroundWorker, which launches when the app starts. Hopefully, when the user requests the data, the BackgroundWorker has completed.
In some cases, however, it might not have. In these cases I want to wait for the loading to complete before showing anything to the user.
All the techniques I can think of have race conditions: for example, if I set up an AutoResetEvent
, I can't use WaitOne()
when the user requests the data, because the AutoResetEvent
might already have signaled; if I add a boolean loading_complete
flag and check it before calling WaitOne, loading_complete
might be set to true after the ckecing, but before the WaitOne call, which will never return...
Any idea?
EDIT: Thanks to @500-InternalServerError for the solution; using a ManualResetEvent
works great. Thanks to everyone else for the suggestions.