Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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?
 
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 3-Jun-13 6:10am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Comments
joshrduncan2012 at 3-Jun-13 11:22am
   
1) It's created in design view on the form itself.
2) WorkerSupportsCancellation is set to true.
Pheonyx at 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 at 3-Jun-13 12:45pm
   
Thanks Pheonyx!
Pheonyx at 3-Jun-13 17:33pm
   
No problem, if my help solved the issue, please mark the solution as such, and rate it please :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you want to cancel it, perhaps you should be setting and testing the same values?
     this.Cancelled = true;
 ...
     this.m_Cancel = true;
     if(this.Cancel)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 175
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 3 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100