Working working C # does not fire a dowork event on any computer other than my own

I have a very strange problem with a background worker in one of my applications. I recently revised it a bit with some GUI fixes, but now when someone on another PC launches .exe or installs my OneClick deployment, the background worker is not in the dowork event. The logic has not changed between the two versions. I made a comparison and, in addition, adding more errors, nothing has changed.

I got to the inclusion of message boxes and breakpoints in the dowork event, but it never gets into it anywhere except my own computer. Even when remote debugging is not included in the DoWork event

Any suggestions?

The click button even triggers the runworkerasync event

private void ScanButton_Click_1(object sender, RoutedEventArgs e) { Scan = new BackgroundWorker(); Scan.WorkerReportsProgress = true; Scan.DoWork += new DoWorkEventHandler(Scan_DoWork); Scan.ProgressChanged += new ProgressChangedEventHandler(Scan_ProgressChanged); Scan.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Scan_RunWorkerCompleted); Scan.RunWorkerAsync(); } 

Below is the code that fires the DoWork event. Ive removed the other functions that it does for simplicity. All he does is run a function that returns a string and puts it in a list called scanresults.

 private BackgroundWorker Scan; public void Scan_DoWork(object sender, DoWorkEventArgs e) { System.Windows.Forms.MessageBox.Show("Inside the DoWork"); BackgroundWorker bw = sender as BackgroundWorker; float percentageDone = 0.0F; ScanResults = new List<string>(); try { System.Windows.Forms.MessageBox.Show("Doing first scan check"); ScanResults.Add(Functions.LocalComputerName()); percentageDone = ((1 / 1f) * 100f); bw.ReportProgress((int)percentageDone); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message, "Error Encountered", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } public void Scan_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { 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); } } 

It completely ignores the dowork event and enters the runworkercomplete event and passes an error for the index, obviously because the list was not populated because the function was skipped. Again, this works fine on my PC, but not on others, no matter how I release them.

Thanks for any help

+4
source share
1 answer

I assume your DoWork throws an exception and therefore RunWorkerCompleted is RunWorkerCompleted .

Note that exceptions thrown in the DoWork BGW method will not be caught in try ... catch in RunWorkerCompleted ; instead, the template should check to see if the Error property in RunWorkerCompleted RunWorkerCompletedEventArgs is null. If it is not null, you have an exception.

You can redo your RunWorkerCompleted code as follows:

 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 information and a better example than mine.

+13
source

Source: https://habr.com/ru/post/1382840/


All Articles