取消e.result中的后台工作程序异常 [英] cancel background worker exception in e.result
本文介绍了取消e.result中的后台工作程序异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的后台工作者遇到了严重的问题.如果任务正常结束,则代码工作正常.当我取消后台任务时,在e.Result的RunWorkerCompleted函数中得到了system.invalidoperationexception.怎么了?谢谢.
i have a serious problem with background worker. code is working if task is ending regular. when i cancel the background task i get an system.invalidoperationexception in the RunWorkerCompleted function for e.Result. what is wrong? thank you.
这是我的鳕鱼:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (backgroundWorker.CancellationPending == true)
e.Cancel = true;
e.Result = resultList;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
List<Object> resultList = (List<Object>)e.Result;
}
推荐答案
这是设计使然,当取消取消DoWork或引发异常时,将抛出Result属性getter.只需检查一下即可:
This is by design, the Result property getter will throw when DoWork was cancelled or threw an exception. Simply check for that:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null) {
List<Object> resultList = (List<Object>)e.Result;
// etc..
}
}
这篇关于取消e.result中的后台工作程序异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文