C# Background worker setting e.Result in DoWork and getting value back in WorkCompleted

后端 未结 3 1787
野的像风
野的像风 2020-12-18 19:52

C# 2008 SP1

I am using the background worker

If one of the conditions fails I will set e.cancel to true, and assign the string to the e.result. Everything wo

3条回答
  •  攒了一身酷
    2020-12-18 20:20

    From MSDN:

    If the operation completes successfully and its result is assigned in the DoWork event handler, you can access the result through the RunWorkerCompletedEventArgs.Result property.

    and:

    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.

    So if it doesn't complete successfully (i.e. you cancel it) it looks like it won't work. Perhaps consider returning your cancellation-details as the result (as success) for your abort case, an detecting the difference in the completion handler?

提交回复
热议问题