How to make an async call when my windows phone application is closing
In my Windows Phone 7 application I want to make a WCF call when the application is closing. However, when I make the call, the application ends-up in :
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
Debugger.Break();
}
}
When I inspect the exception message it's empty, but there is this stack trace.
at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously)
at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously, Exception exception)
at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetO开发者_如何学JAVAutputStreamAsyncResult.OnGetRequestStream(IAsyncResult result)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass6.<InvokeGetRequestStreamCallback>b__4(Object state2)
at System.Threading.ThreadPool.WorkItem.doWork(Object o)
at System.Threading.Timer.ring()
I tried to add try/catch block around my call but it still ends up the same way.
How can I make the call successfully ?
Nope, you can't make an async call when the app is closing. If you absolutely must send something when the app is closing you would have to write it to isolated storage and then send it the next time the app launches - this is what some of the analytic frameworks do to record session end times.
As you've shown the unhnadled excpetion handler in your code I'm assuming that you want to make the call when there is such an exception, not just every time the app closes.
Don't!
If something happens to cause an unhandled exception you should save the details and then send them the next time the app is started. You don't know how long the send will take and you app may be torn down during this time.
Lots of people make this mistake when opening the EmailComposeTask in the unhandled exception handler.
精彩评论