.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

This BackgroundWorker states that it doesn't support cancellatio

hey i get the below exception when i am using background worker my code looks like this:--

Exception i got:-----

This BackgroundWorker states that it doesn't support cancellation

Mycode:-----------

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
BackgroundWorker bw = sender as BackgroundWorker;
someclass obj = new someclass ();
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = obj.startprocess(someid, bw, e);
if (bw.CancellationPending)
{
                e.Cancel = true;
}}



public bool startprocess(String someid, BackgroundWorker bw, DoWorkEventArgs e)
        {
            bool bRet = false;
            while (!bw.CancellationPending)
            {


                try
                {
                    somecode
                    if (bRet)
                    {
                      processing code
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
                return bRet;
           
        }
newbie
Wednesday, January 24, 2007
 
 
outshider
Wednesday, January 24, 2007
 
 
BackGroundWorker.SupportsCancellation = true;// or something like that

This should be set!
J Send private email
Wednesday, January 24, 2007
 
 
Set the WorkerSupportsCancellation property of the BackgroundWorker to true. It is false by default. You will probably also want to set the WorkerSupportsProgress property to true as well if you want to use the progress update capability.
dood mcdoogle
Wednesday, January 24, 2007
 
 
hey the  function    public bool startprocess(String someid, BackgroundWorker bw, DoWorkEventArgs e) is in another package
not in window form.... when i click the cancel event the process is not getting stopped??????????????
newbie
Wednesday, January 24, 2007
 
 
You need to physically stop it yourself. It isn't going to automatically stop for you. The loop doing the work needs to check the CancellationPending property to know if it needs to abort.

Take a look at the example on MSDN. You can't expect to use this component without doing a little bit of reading on your own.
dood mcdoogle
Wednesday, January 24, 2007
 
 
For a pretty decent overview of the whole "threading" thing, see http://www.thescripts.com/forum/threadnav260780-2-10.html

(though this does not deal with the backgroundworker...you get to see all the other thorny prickly pieces that you don't have to worry about plumbing WITH the background worker...!)

Elsewise the comments made above are correct in my experience.  You have to not only set the backgroundworker to support cancellation, you have to handle the cancel event.

http://www.learnvisualstudio.net has a short video on how the backgroundworker (err) works.  (as a matter of fact, it was one of the free ones recently if you were subscribed to his email list)  If you've got a (small) budget this site is a great resource.  The videos are well done, to the point, and (in my opinion) much more efficient than slogging through MSDN...
(3 month membership is $40,  year=$70, life=$100)

(Not affiliated, just a happy user, BTW)

-aaron
http://www.itgroundhog.com
aaron Send private email
Wednesday, January 24, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz