C# background worker not triggering dowork event on any pc besides my own

微笑、不失礼 提交于 2019-12-05 06:18:37

My guess is that your DoWork is throwing an exception and so your RunWorkerCompleted is called.

Note that exceptions thrown in a BGW's DoWork method will not be caught in a try...catch in RunWorkerCompleted; instead, the pattern is to check if the Error property in RunWorkerCompleted's RunWorkerCompletedEventArgs parameter is not null. If this is not null, you have an exception.

You might rework your RunWorkerCompleted code like this:

public void Scan_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
    if (e.Error != null) {
        // You have an exception, which you can examine through the e.Error property.
    } else {
        // No exception in DoWork.
        try {
            if (ScanResults.Count == 0) {
                System.Windows.Forms.MessageBox.Show("Empty");
                return;
            }
            MachineNameBox.Text = ScanResults[0];
        } catch (Exception ex) {
            System.Windows.MessageBox.Show(ex.Message, "Error Encountered", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }
    }
}

See BackgroundWorker.RunWorkerCompleted Event for more info and a better example than mine.

For me it was that my main project was signed and the new DLL I brought in was not. It's weird because I never got a warning or error and the project compiled ok. Just that the background_dowork() would never even get hit in debug. Once I added in the signing cert on the DLL it started that backgroundworker just fine.

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