How to close BackgroundWorker stream when application is disconnected?

I create a stream with BackgroundWorker, and in a loop, I check every time if it is CancellationPendingtrue or not, for example:

   public MainPage()
    {
        InitializeComponent();

        bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
    }

    private void ButtonStart_Click(object sender, RoutedEventArgs e)
    {
        if (bw.IsBusy != true)
        {
            bw.RunWorkerAsync();
        }
    }

    private void ButtonCancel_Click(object sender, RoutedEventArgs e)
    {
        if (bw.WorkerSupportsCancellation)
        {
            bw.CancelAsync();
        }
    }

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 1; i <= 100; i++)
        {
            Debug.WriteLine("The tread is working");
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                bw.CancelAsync();
                break;
            }
            else
            {

                System.Threading.Thread.Sleep(500);
                worker.ReportProgress(i);
            }
        }
    }

    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled)
        {
            tbProgress.Text = "Canceled";
        }
        else if (e.Error != null)
        {
            tbProgress.Text = "Error: " + e.Error.Message;
        }
        else
        {
            tbProgress.Text = "Done";
        } 
    }

    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        tbProgress.Text = e.ProgressPercentage.ToString() + "%";
    }

When the application is deactivated, the thread was not closed, it is interrupted and an exception is thrown. How to close streams with BackgroundWorkerwhen the application is disconnected?

+5
source share
4 answers

Have you set BackgroundWorkerto cancel?

 var bg= new BackgroundWorker();
 bg.WorkerSupportsCancellation = true;

From the documentation:

WorkerSupportsCancellation true, , BackgroundWorker . , CancelAsync .

, CancelAsync() , CancellationPending, .

100%, , bw. `

+5

, , , ThreadAbortException, . , " " , . ThreadAbortException , , , ThreadAbortException catch. finally .

BackgroundWorker, , ThreadAbortException . - , , ThreadAbortException bw_DoWork, , , .

, , , .

+10

CancellationPending , .
? ""? CancelAsync (...). CancellationPending , , - ResetEvent.

+2

, . / .

, :

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    if (e.IsApplicationInstancePreserved)
    {
        Log.write("Activating, instance preserved");
        // restart long-running network requests
        startBackground();
    }
    else // start over again
    {
        AppSettings.init();
        Log.write("Activating, rehydrating" );
        startBackground();
    }
}

- BackgroundWorker . , , ", ", BackgroundWorker.

, , , , ?

+1

All Articles