C# BackGroundWorker Anomaly DoWork event not firing

与世无争的帅哥 提交于 2019-12-11 10:52:22

问题


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"); 
}

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

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

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.


回答1:


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

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




回答2:


I'm not sure what the problem was but this resolved the issue.

Initialy I had just copied the business.dll into the bin folder and referenced it, however the folder contained an older business.obj file. When I copied both the business.dll and business.obj file the issue went away.



来源:https://stackoverflow.com/questions/950907/c-sharp-backgroundworker-anomaly-dowork-event-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!