我正在处理一个C#项目,在其中我有一个Backgroundworker来执行我的“耗时任务”。
在我的“DoWork”中,我想通过“backgroundworker.ReportProgress(some int)”来报告进度。但是当我的程序调用“backgroundworker.ReportProgress(some int)”时,我会收到一个“System.Reflection.TargetInvocationException”的错误。
我该如何解决这个问题?
在我的“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;