Let me Ask
Let me Ask

Reputation: 1039

Windows Phone 7 - Handle Error in Application_UnhandledException with WebClient

I need to handle exception in Application_UnhandledException event of App.xaml.cs file. I am getting the Exception object using e.ExceptionObject. Now, I need to send the exception details to my server using WCF service with the help of WebClient.

Whenever I send request to WCF service using WebClient, it send the request but the callback event e.g. webClient_UploadStringCompleted never gets executed. I read that the exceptions are handled in separate thread etc.

What I have already tried, but not succeeded:

  1. ThreadStart
  2. Thread
  3. App.Current.RootVisual.Dispatcher.BeginInvoke
  4. RootFrame.Dispatcher.BeginInvoke
  5. System.Windows.Deployment.Current.Dispatcher.BeginInvoke
  6. Deployment.Current.Dispatcher.BeginInvoke

Can anybody tell, how to send the error details from Application_UnhandledException to WCF service with WebClient?

Code:

// Code to execute on Unhandled Exceptions

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{

e.Handled = true;

// Option 1
Thread thread = new Thread(() => MainPage.HandleException(e.ExceptionObject));
thread.Start();
ThreadStart start = new ThreadStart(NonUIWork);

// Option 2
Thread thread = new Thread(start);
thread.Start(e.ExceptionObject);

// Option 3
Deployment.Current.Dispatcher.BeginInvoke(delegate { MainPage.HandleException(e.ExceptionObject); }); 

// Option 4
App.Current.RootVisual.Dispatcher.BeginInvoke(MainPage.HandleException, e.ExceptionObject);

// Option 5
RootFrame.Dispatcher.BeginInvoke(() => { MainPage.HandleException(e.ExceptionObject); });

// Option 6
MainPage.HandleException(e.ExceptionObject); 
}

In MainPage.HandleException method, I am sending exception details to WCF service with WebClient object. But, the call back function of WebClient (webClient_UploadStringCompleted) never gets executed. It seems that the thread in which the Application_UnhandledException event is executed is getting suspended immedietely.

Upvotes: 2

Views: 2341

Answers (2)

Sumit
Sumit

Reputation: 21

you can store the exception using IsolatedStorage in windows phone and when application re-launches you can send error description using asyncronous call

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 941635

Yes, this can't work by design. The event is raised while the CLR is shutting down the AppDomain, just before it gets unloaded. You cannot expect any thread or asynchronous operation to run to completion. All your code must be done by the time your event handler exits. Be sure to use synchronous methods and avoid using threads all together. There isn't any point in the usual don't-block-the-UI code, there is no UI anymore by the time the event is raised.

This is indeed an issue on Windows Phone, it doesn't have the synchronous methods of WebClient, like WebClient.UploadString(). You'll have to make UploadStringAsync() synchronous yourself by using an AutoResetEvent. Call its WaitOne() method after the upload call, call its Set() method in the callback.

Upvotes: 1

Related Questions