Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / May 2008

Tip: Looking for answers? Try searching our database.

CancellationPending Always False

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Krip - 14 May 2008 14:16 GMT
I'm writing a WinForms app in .NET 3.0 using the BackgroundWorker component.
I call the CancelAsync method, and in another method check
CancellationPending.  CancellationPending is always false.  Anyone else have
this?  Or know why this is?

Thanks,
Krip
Morten Wennevik [C# MVP] - 15 May 2008 06:35 GMT
Hi Krip,

Did you set BackgroundWorker.WorkerSupportsCancellation = true?

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> I'm writing a WinForms app in .NET 3.0 using the BackgroundWorker component.
> I call the CancelAsync method, and in another method check
[quoted text clipped - 3 lines]
> Thanks,
> Krip
Krip - 15 May 2008 11:07 GMT
> Did you set BackgroundWorker.WorkerSupportsCancellation = true?

Yep.
Morten Wennevik [C# MVP] - 15 May 2008 13:11 GMT
> > Did you set BackgroundWorker.WorkerSupportsCancellation = true?
>
> Yep.

Then I'm afraid we will need to see some code to be able to help you.  
CancelAsync should set worker.CancellationPending.  I haven't tested if it
returns true in the GUI thread, but it should definitely return true inside
the worker thread.

The below code sample should demonstrate how cancellation should work. It
will run a background worker for ten seconds unless cancelled by clicking the
"Cancel" button.

   public partial class Form1 : Form
   {
       BackgroundWorker worker = new BackgroundWorker();
       public Form5()
       {
           InitializeComponent();

           worker.DoWork += new DoWorkEventHandler(worker_DoWork);
           worker.WorkerSupportsCancellation = true;

           Button bt = new Button();
           bt.Text = "Cancel";
           Controls.Add(bt);
           bt.Click += new EventHandler(bt_Click);

           worker.RunWorkerAsync();
       }

       void bt_Click(object sender, EventArgs e)
       {
           worker.CancelAsync();
       }

       void worker_DoWork(object sender, DoWorkEventArgs e)
       {
           DateTime start = DateTime.Now;
           BackgroundWorker worker = sender as BackgroundWorker;

           while ((DateTime.Now - start).TotalSeconds < 10)
           {
               if (worker.CancellationPending)
               {
                   MessageBox.Show("Cancelled");
                   return;
               }
               Thread.Sleep(1000);
           }
           MessageBox.Show("Finished");
       }
   }

Signature

Happy Coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.