【发布时间】:2011-11-22 14:46:51
【问题描述】:
我正在从事一个 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 eventhandler,不知道必须手动设置:
bgrndWorkerSearchMatches.WorkerReportsProgress = true;
【问题讨论】:
-
您可能希望将调试器设置为中断所有异常。
标签: c# multithreading exception backgroundworker