Note that there are some explanatory texts on larger screens.

plurals
  1. POBackgroundWorker works first time, but fails the second time
    text
    copied!<p>Im having an odd issue with my implementation of a BackgroundWorker class. It is inside my program that will run some tests (for some hardware which is not important for this question). Here is my code for the BackgroundWorker:</p> <pre><code>public AllTests(MainMenu p) { InitializeComponent(); WindowStartupLocation = WindowStartupLocation.CenterScreen; this.parent = p; generateCaseLabels(tc.getTCs()); var testThread = new BackgroundWorker(); // Tell the testthread what to do testThread.DoWork += (sender, args) =&gt; { List&lt;BaseTestCase&gt; cases = tc.getTCs(); foreach ( BaseTestCase test in cases ) { test.execute(); while (!test.Done) { Thread.Sleep(500); } if (test.Passed) { passed.Add(test); } else { failed.Add(test); } } }; // Start the TestReport after the testing is completed testThread.RunWorkerCompleted += (sender, args) =&gt; { TestReport newWindow = new TestReport(parent, passed, failed); testThread.CancelAsync(); // reset the testThread (?) log.append(this, "--- DONE TESTING --------------------"); newWindow.Show(); this.Close(); }; // Start the testthread testThread.WorkerSupportsCancellation = true; testThread.RunWorkerAsync(); } </code></pre> <p>The odd thing that happens with this code is when started for the first time it works great, but when again navigation to this <code>AllTests</code> window the <code>TestReport</code> window is instantly opened, indicating that the <code>testThread.RunworkerCompleted</code> is immediately called. So my question: Does anyone know a smart thing to do with resetting the BackgroundWorker or something, or have an answer for this weird behavior?</p> <p>BTW here's how i call the AllTests Window:</p> <pre><code> private void startTests(object sender, RoutedEventArgs e) { log.append(this, "--- STARTING ALL TESTS --------------------"); AllTests newWindow = new AllTests(parent); newWindow.Show(); this.Close(); } </code></pre> <p>Thanks for your help! Robbert.</p> <p>PS If i made any spelling errors or the question is not clear enough; please excuse me- english is not my native language and this is my first post on StackOverflow! Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload