加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么我用BackgroundWorker.ReportProgress获取TargetInv

发布时间:2020-12-16 00:01:19 所属栏目:百科 来源:网络整理
导读:我正在开发一个C#项目,我有一个Backgroundworker来完成我的“昂贵的工作”. 在我的“DoWork”中,我想通过“backgroundworker.ReportProgress(some int)”来报告进度.但是当我的程序调用“backgroundworker.ReportProgress(some int)”时,我得到一个“System.
我正在开发一个C#项目,我有一个Backgroundworker来完成我的“昂贵的工作”.

在我的“DoWork”中,我想通过“backgroundworker.ReportProgress(some int)”来报告进度.但是当我的程序调用“backgroundworker.ReportProgress(some int)”时,我得到一个“System.Reflection.TargetInvocationException”.

我该如何解决我的问题?

private void btnGrap_Click(object sender,EventArgs e)
    {
        //some code
        ListsObject listsObject = new ListsObject(filePaths,enumList);
        progressBar1.Maximum = 100;//count;
        this.bgrndWorkerSearchMatches.RunWorkerAsync(listsObject);
     }

_做工作:

private void backgroundWorkerSearchMatches_DoWork(object sender,DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            bgrndWorkerSearchMatches.ReportProgress(i);
        }
     }

_ProcessChanged:

private void bgrndWorkerSearchMatches_ProgressChanged(object sender,ProgressChangedEventArgs e)
    {
        //progressBar1.Value = e.ProgressPercentage;
    }

我找到了答案:

我用Visual Studio创建了backgroundworker eventhandler,并且不知道我必须手动设置:

bgrndWorkerSearchMatches.WorkerReportsProgress = true;

解决方法

抛出TargetInvocationException以包装最终调用的方法抛出的另一个异常.

检查InnerException以找出发生的情况.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读