Friday 15 August 2014

multithreading - C# BackgroundWorker Wait Until Ends -


I am working in projects that need to be done with threads.

If I hit the button 3 , which works for the background worker, then my background worker will have to restart. I have already tried waiting for the background work with Backgroundworker1.IsBusy , but it never ends when I press the button3 , I get an error

This background player is currently busy and can not run multiple tasks together

This is my code:

  Public Partial Sections Form 1: Form {Public Form 1 () {Initial Group (); } Private Zero Button 1_Click (Object Sender, EventArgs e) {backgroundWorker1.RunWorkerAsync (); } Private Zero Button 2_Click (Object Sender, EventArgs e) {backgroundWorker1.CancelAsync (); } Private Zero Button 3_Click_1 (Object Sender, EventArgs e) {backgroundWorker1.CancelAsync (); BackgroundWorker1.RunWorkerAsync (); } Private zero background by 1_DoWork (Object Sender, DoWorkEventArgs e) {BackgroundWorker worker = Sender as BackgroundWorker; Int x = 0, y = 0, result; While (true) {if (backgroundWorker1.CancellationPending) {e.kernel = true; break; } And {x ++; Y ++; Results = x + y; This.label1.Invoke (new method Invoker (() => this.label1.Text = result.tostring ()); }}}} // Public Partial Class Form 1: Form    

for the full event Listen, and then restart the worker there -

  private bool restartOnCancel = false; Private Zero Button 3_Click_1 (Object Sender, EventArgs e) {restartOnCancel = true; BackgroundWorker1.CancelAsync (); } Private Zero Background Worker 1_Worker (Object Sender, Runwalker Completed Event Events E) {If ((E. Canceled == Truth) & amp; Restart Cancel on) {restartOnCancel = false; BackgroundWorker1.RunWorkerAsync (); }}    

No comments:

Post a Comment