C#BackGroundWorker异常DoWork事件未触发 [英] C# BackGroundWorker Anomaly DoWork event not firing

查看:450
本文介绍了C#BackGroundWorker异常DoWork事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的设置,此代码正常工作

This is the setup I have, this code works properly

private void butGo_Click(object sender, EventArgs e)
{
  threadCreateImages.RunWorkerAsync();
}


private void threadCreateImages_DoWork(object sender, DoWorkEventArgs e)
{
  PatientToHL7MSHManager tvPatientToHL7MSHManager = new PatientToHL7MSHManager();
  tvPatientToHL7MSHManager.LoadByMSHID(""); 
}


private void threadCreateImages_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
  MessageBox.Show("DONE"); 
}

如果我改变 这条线 tvPatientToHL7MSHManager.LoadByMSHID("); 到 tvPatientToHL7MSHManager.LoadByPatientID(");

if I change this line tvPatientToHL7MSHManager.LoadByMSHID(""); to tvPatientToHL7MSHManager.LoadByPatientID("");

它跳过DoWork事件,直接进入RunWorkerCompleted事件.

It skips over the DoWork event and goes straight to the RunWorkerCompleted event.

LoadByMSHID和LoadByPatientID之间的唯一区别是SQL语句上的过滤器,除了代码路径相同之外.

The only difference between the LoadByMSHID and the LoadByPatientID is the filter on the SQL statement besides that the code path is identical.

该代码在没有后台线程的情况下也可以正常工作.

The code does work properly without the background thread.

任何想法或建议都将不胜感激.

Any ideas or suggestions would be very appreciated.

推荐答案

很可能会引发异常.在您的RunWorkerCompleted事件中,检查错误属性传递的RunWorkerCompletedEventArgs值.

Likely there's an exception being thrown. Within your RunWorkerCompleted event, check the Error property of the RunWorkerCompletedEventArgs value being passed in.

BackgroundWorkers在发生异常时不会向主线程引发异常.相反,您必须在RunWorkerCompleted事件中检查它们.

BackgroundWorkers do not raise exceptions up to the main thread when they occur. Instead, you have to check for them on the RunWorkerCompleted event.

这篇关于C#BackGroundWorker异常DoWork事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆