Reputation: 615
I'm using a background worker in order to get data from a database. My problem is that I'm struggling to report any errors in the database activity.
I've tried straight up spawning a Dialog (Windows Form that I've created) from the DatabaseUpdater class. However, this hangs and I'm left with a white box instead of the error message. I've tried spawning the Dialog in a separate thread - this results in the Dialog appearing and disappearing almost instantly. Obviously I wasn't entirely surprised by this, but attempts at maintaining the Dialog resulted in the same white box effect.
I guess my question is really what is the best practice for displaying errors coming from threaded activity?
Upvotes: 1
Views: 466
Reputation: 1191
Possibly not what you are looking for, but you might get some mileage out of the InvokeRequired property and Invoke methods on your main form.
Upvotes: 0
Reputation: 1115
Assuming the BackgroundWorker task in invoked from the UI thread, you should check for and display any errors in the handler for the RunWorkerCompleted event - do not try to handle them in the DoWork handler method...
Upvotes: 1
Reputation: 18994
This is a good resource for multithreading and WinForms: Synchronizing calls to the UI in a multi-threaded application
Upvotes: 2