473,322 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

CancelAsync not working..

I am pasting a code which i have made in VS...al is fine bt cancelasync method of background worker is not working..so please help me out what is missing.



Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace AsyncDemo
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.             backgroundWorker1.WorkerReportsProgress = true;
  20.             backgroundWorker1.WorkerSupportsCancellation = true;
  21.             backgroundWorker1.DoWork += new DoWorkEventHandler(bw_DoWork);
  22.             backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
  23.             backgroundWorker1.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(bw_cmplt);
  24.         }
  25.  
  26.         private void bw_cmplt(object sender, RunWorkerCompletedEventArgs e)
  27.         {
  28.             if (e.Cancelled)
  29.                 MessageBox.Show("Task Cancelled");
  30.             else if (e.Error != null)
  31.                 MessageBox.Show("Some Error Occured");
  32.             else
  33.                 MessageBox.Show("Task Completed");
  34.         }
  35.         private void bw_DoWork(object sender, DoWorkEventArgs e)
  36.         {
  37.             if (backgroundWorker1.CancellationPending)
  38.             {
  39.                 e.Cancel = true;
  40.             }
  41.             else
  42.             {
  43.                 for (int i = 0; i < 100; i++)
  44.                 {
  45.                     backgroundWorker1.ReportProgress(i);
  46.                     Thread.Sleep(50);
  47.                 }
  48.             }
  49.         }
  50.         private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
  51.         {
  52.             progressBar1.Value = e.ProgressPercentage;
  53.         }
  54.  
  55.         private void button1_Click(object sender, EventArgs e)
  56.         {
  57.             backgroundWorker1.RunWorkerAsync();
  58.         }
  59.  
  60.         private void button2_Click(object sender, EventArgs e)
  61.         {
  62.             backgroundWorker1.CancelAsync();
  63.  
  64.         }
  65.  
  66.     }
  67. }
  68.  
Jul 24 '12 #1
0 1797

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gary | last post by:
I am trying to use the "System.Windows.Forms.SendKeys" class for triggering the Ctrl+P key. Syntax: System.Windows.Forms.SendKeys.Send("^(P)"); This is not working ..what could be the...
6
by: Mullin Yu | last post by:
hi, i have a web service that has file operations on Windows OS, and there may be a file concurrency issue if only one working directory e.g. c:\working therefore, i want to have a unique sub...
3
by: | last post by:
Hello, I am hoping someone else has thought about a date time calculation i need to perform. I would like to be able to calculate the number of "working minutes" between 2 dates, given my...
5
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
5
by: tshad | last post by:
I have been working with setting my drop boxes to allow double clicking to select an item. It worked fine until I made some changes. I then stripped the page down to the bare essentials to find...
8
by: jojobar | last post by:
Okay, I am trying to do is to test the webresource in 2.0 1. I created a new project with assembly name (and default assembly name) "Office". 2. I added the following to the AssemblyInfo.cs...
2
by: Don | last post by:
I'm having problems with intellisense, autocomplete, etc. suddenly not working in certain classes of a project I'm working on. All the options are set, and it all works fine for most classes, but...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
0
by: Neil Moss | last post by:
Hi all I have a SoapExtension that is monitoring various aspects of all web service activity from my WinForms client. From within the extension, is there any way that I can detect that an...
1
by: deerchao | last post by:
I found a thread (http://groups.google.com/group/ microsoft.public.dotnet.languages.csharp/browse_thread/thread/ b154f44522b5e083/11974fe21507faae?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.