scr*_*cro 3 c# multithreading exception backgroundworker
我正在开发一个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);
}
Run Code Online (Sandbox Code Playgroud)
_做工作:
private void backgroundWorkerSearchMatches_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
bgrndWorkerSearchMatches.ReportProgress(i);
}
}
Run Code Online (Sandbox Code Playgroud)
_ProcessChanged:
private void bgrndWorkerSearchMatches_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//progressBar1.Value = e.ProgressPercentage;
}
Run Code Online (Sandbox Code Playgroud)
我找到了答案:
我用Visual Studio创建了backgroundworker事件处理程序,并且不知道我必须手动设置:
bgrndWorkerSearchMatches.WorkerReportsProgress = true;
Run Code Online (Sandbox Code Playgroud)