我正在开发一个 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);
}
_DoWork:
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 事件处理程序并且不知道我必须手动设置:
bgrndWorkerSearchMatches.WorkerReportsProgress = true;
最佳答案
抛出 TargetInvocationException
以包装最终调用的方法抛出的不同异常。
检查 InnerException
以了解发生了什么。
关于c# - 为什么我会得到带有 BackgroundWorker.ReportProgress 的 TargetInvocationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8228687/