简体   繁体   中英

Cancelling backgroundworker on application_deactivated event WP7

I am having a little bit of an issue with cancelling my background worker in the Application_Deactivated event on Windows Phone 7.

My background worker is syncing with a web service, but as I can't guess how long its going to take I just want to cancel the worker. Problem with this is that after the event handler is done the application will just kill all threads so I can't cancel it properly.

What I would like to do is something like this:

Application_Deactivated 
{
    Backgroundworker.RunWorkerCompleted += bw_completed;
    Backgroundworker.Cancel async

    // wait till completed event which confirm the background thread has been cancelled
}

I first tried to use a AutoResetEvent in the event handler but this won't work cause the App.xaml.cs Thread is the main thread of the application and this waiting in this thread will block the entire application.

After the cancelAsync has been called in the event handler, the application will just go dormant and when resuming the web handler that is called in the actual background worker will give an error about a null reference to a web response.

I am already checking for cancellation pending in the background worker and the cancel functionality has been tested with a cancel button, so that ain't the problem. Its purely that I need to wait till the background worker has exited properly.

I found some results on this site like this one: How close BackgroundWorker thread when application is deactivated?

But that only gave a solution with the ThreadAbortException but I rather don't use this one.

If you need some more code I can provide it.

Your threads are being shut down because they're background threads. When an app shuts down all the background threads get shutdown and there's nothing you can do about this.

You can create a foreground thread. The app won't/can't kill a foreground thread and will run until the foreground thread terminates.

To execute your call using this method you'd need to do something like this:

Application_Deactivated 
{
    thread t=new Thread(CancelCall);
    thread.IsBackground=false;//make this a foreground thread
    thread.Start();
}

private void CancelCall(object state)
{
    Backgroundworker.RunWorkerCompleted += bw_completed;
    Backgroundworker.Cancel async
    Thread.Sleep(10000); wait for 10 seconds
}

It's worth mentioning that the background thread created by the BackgroundWorker is going to be shut down by the app anyway so I'm not sure what extra you're going to get by doing this. A 10 second shutdown isn't going to make the users happy, they'll most likely complain that the app is a) slow or b) has a bug.

Unless something is actively going "bang" and you're getting bad user feedback or error reports I'd suggest leaving this as is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM