Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!

This is my first attempt on a background worker implementation. I am trying to cancel the background worker events on a button click. The current activity is that the cancellation of the do_work background events never happens but it goes on to the next events as far as hiding the form and opening the new one.

Can someone shed some light as to what I'm doing wrong or what I'm missing to be able to cancel the background activities on button click?

C#
bool networkStatus = NetworkInterface.GetIsNetworkAvailable();
private bool m_Cancel = false;
public bool Cancelled { get; set; }

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
     if (networkStatus == true)
     {
          outsetUpdates();                
     }
     else
     {
          this.Visible = false;
          LauncherForm lForm = new LauncherForm();
          lForm.Show();
          return;
     }

     if(this.Cancel)
     {
          MessageBox.Show("Update cancelled.");
          this.Visible = false;
          e.Cancel = true;
          LauncherForm mainForm = new LauncherForm();
          mainForm.Show();
          return;
     }
}

private void BeginningUpdaterCheck_Load(object sender, EventArgs e)
{
     backgroundWorker1.RunWorkerAsync();
}

private void skipToFormButton_Click(object sender, EventArgs e)
{
     this.Cancelled = true;

     this.Hide();
     LauncherForm mainForm = new LauncherForm();
     this.m_Cancel = true;
     mainForm.Show();
     return;
}
Posted

Question 1) Where do you instantiate the backgroundWorker1 ?
Question 2) Do you set WorkerSupportsCancellation = true on the background worker?

The example on the MSDN website for background worker shows how to create a cancellable background worker:

http://msdn.microsoft.com/en-us/library/4852et58.aspx[^]
 
Share this answer
 
Comments
joshrduncan2012 3-Jun-13 11:22am    
1) It's created in design view on the form itself.
2) WorkerSupportsCancellation is set to true.
Pheonyx 3-Jun-13 11:38am    
Which ever button you have that is supposed to cancel the background worker should have the following in its click event:


if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Cancel the asynchronous operation.
backgroundWorker1.CancelAsync();
}

Then in the method that your DoWork event calls that runs on the separate thread (I suspect is outsetUpdates(); but I don't know for sure) needs to check for the following:

if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}

The backgroundWorker1_DoWork event handler will run through once, so it is unlikely that this will catch you cancelling the background worker based on how you have written it.
joshrduncan2012 3-Jun-13 12:45pm    
Thanks Pheonyx!
Pheonyx 3-Jun-13 17:33pm    
No problem, if my help solved the issue, please mark the solution as such, and rate it please :)
If you want to cancel it, perhaps you should be setting and testing the same values?
C#
    this.Cancelled = true;
...
    this.m_Cancel = true;

C#
if(this.Cancel)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900