Question About BackgroundWorker.RunWorkerCompleted

pisceswzh

Well-known member
Joined
Mar 19, 2007
Messages
96
Programming Experience
1-3
I use the BackgroundWorker to perform multi-thread process to increase user experience. However if users close the window before the BackgroundWorker finishes working and when it does finish and try to return values to the controls on the form, it will throw a exception: Object reference not set to an instance of an object. I understand because the window has already been closed.

I am just wondering if there is any good approach to avoid this exception since it is really meanless to continue return the result when the window has already been closed.

Thanks.

Tom
 
If it's possible to abort the background operation then you can use the cancellation functionality built into the BackgroundWorker. Otherwise you should test the IsDisposed property of the form in the RunWorkerCompleted event handler and simply ignore the results if it's True.
 
Back
Top