C# background worker not triggering dowork event on any pc besides my own
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.