none
How stop backgroundworker RRS feed

  • Question

  • Hello ,
    I am using background worker to call web service procedures , If  the background worker is running , I use CancelAsync(); method to stop it , but it still running until the call of the web
    service procedure  finished , Is there any solution to stop the background worker even it is running ?
    Saturday, May 30, 2009 9:55 AM

Answers

  • private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
    
        for (int i = 1; (i <= 10); i++)
        {
            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
                break;
            }
            else
            {
                // Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500);
                worker.ReportProgress((i * 10));
            }
        }
    }
    
    private void buttonCancel_Click(object sender, RoutedEventArgs e)
    {
        if (bw.WorkerSupportsCancellation == true)
        {
            bw.CancelAsync();
        }
    }
    
    


    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    • Proposed as answer by Figo Fei Wednesday, June 3, 2009 3:32 AM
    • Marked as answer by Figo Fei Friday, June 5, 2009 3:23 AM
    Saturday, May 30, 2009 10:40 AM
  • Amal has showed an example of using the CancelAsync.

    I want to explain it better.
    CancelAsync
    will not actually happen until the working thread checks the CancellationPending flag, so the worker code should periodically check the CancellationPending property to see if it has been set to true.
    There is also a good example avail at http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx

    And Roy introduced a CancelImmediately feature in his blog: http://weblogs.asp.net/rosherove/pages/BackgroundWorkerEx.aspx, Hope it helps.

    Thanks.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    • Edited by Figo Fei Monday, June 1, 2009 9:55 AM
    • Proposed as answer by Figo Fei Wednesday, June 3, 2009 3:32 AM
    • Marked as answer by Figo Fei Friday, June 5, 2009 3:23 AM
    Monday, June 1, 2009 9:52 AM

All replies

  • private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
    
        for (int i = 1; (i <= 10); i++)
        {
            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
                break;
            }
            else
            {
                // Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500);
                worker.ReportProgress((i * 10));
            }
        }
    }
    
    private void buttonCancel_Click(object sender, RoutedEventArgs e)
    {
        if (bw.WorkerSupportsCancellation == true)
        {
            bw.CancelAsync();
        }
    }
    
    


    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    • Proposed as answer by Figo Fei Wednesday, June 3, 2009 3:32 AM
    • Marked as answer by Figo Fei Friday, June 5, 2009 3:23 AM
    Saturday, May 30, 2009 10:40 AM
  • Amal has showed an example of using the CancelAsync.

    I want to explain it better.
    CancelAsync
    will not actually happen until the working thread checks the CancellationPending flag, so the worker code should periodically check the CancellationPending property to see if it has been set to true.
    There is also a good example avail at http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx

    And Roy introduced a CancelImmediately feature in his blog: http://weblogs.asp.net/rosherove/pages/BackgroundWorkerEx.aspx, Hope it helps.

    Thanks.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    • Edited by Figo Fei Monday, June 1, 2009 9:55 AM
    • Proposed as answer by Figo Fei Wednesday, June 3, 2009 3:32 AM
    • Marked as answer by Figo Fei Friday, June 5, 2009 3:23 AM
    Monday, June 1, 2009 9:52 AM