backgroundworker pass variable?

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
can you pass variable from dowork to completed?
 
I have tried e.Result, its all fine when the backgroundworker is completed. But if you cancel it, the result will be "Operation has been cancelled" rather than what you set it
 
This is yet another example of not reading the documentation, or at least not reading it properly.
Your RunWorkerCompleted event handler should always check the Error and Cancelled properties before accessing the Result property. If an exception was raised or if the operation was canceled, accessing the Result property raises an exception.
If you set e.Cancel to True in the DoWork event handler then you're saying that the operation was cancelled, so then providing a result makes no sense. If you want to provide a result then you're saying that the operation completed, so you don't set e.Cancel.
 
Back
Top