BackgroundWorker.ReportProgress 突破方法
似乎无论我在BackgroundWorker 上调用ReportProgress,该方法的处理都会停止,因此我的工作永远不会完成。示例:
int numQuals = this.Model.Names.Count();
int currentQual = 0;
int fivePercent = (int)(numQuals * .05);
foreach (var qualName in this.Model.Names)
{
if ((worker != null) && ((currentQual % fivePercent == 0)))
{
worker.ReportProgress((int) (((float)++currentQual / numQuals) * 100));
}
// This next line never processes. I can debug and it will
// break at the ReportProgress line but won't ever break here
this.myContainer.Add(...
}
有谁知道这是为什么吗?
It seems like wherever I call ReportProgress on my BackgroundWorker, the processing of that method stops so my work never finishes. Example:
int numQuals = this.Model.Names.Count();
int currentQual = 0;
int fivePercent = (int)(numQuals * .05);
foreach (var qualName in this.Model.Names)
{
if ((worker != null) && ((currentQual % fivePercent == 0)))
{
worker.ReportProgress((int) (((float)++currentQual / numQuals) * 100));
}
// This next line never processes. I can debug and it will
// break at the ReportProgress line but won't ever break here
this.myContainer.Add(...
}
Does anyone have any idea why this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否设置了
worker.WorkerReportsProgress=true
?否则,后台工作程序将异常终止,并且工作将无法完成。Have you set
worker.WorkerReportsProgress=true
? Otherwise, the background worker will terminate with an exception and the work will not be fullfilled.我建议将您的计算逻辑分离到不同的行。我讨厌从其他开发人员那里看到这样的内联内容。它更难阅读,更难调试(正如您的问题所指出的)。
如果你在 do work 方法中,你的worker 永远不应该为空。尽管我确实建议执行空值检查 - 如果此处存在空值,则其他内容会出现重大错误。
I would recommend separating out your calculating logic to a different line. I hate seeing inline stuff like this from other developers. It's harder to read and harder to debug (as your question points out).
Your worker should never be null if you are inside the do work method. Although I do recommend performing null checks - if a null existed here something else is majorly wrong.