C# Windows Phone 7-处理应用程序中的错误\u WebClient未处理的异常

C# Windows Phone 7-处理应用程序中的错误\u WebClient未处理的异常,c#,silverlight,windows-phone-7,exception-handling,multithreading,C#,Silverlight,Windows Phone 7,Exception Handling,Multithreading,我需要处理App.xaml.cs文件的Application_UnhandledException事件中的异常。我使用e.ExceptionObject获取异常对象。现在,我需要在WebClient的帮助下使用WCF服务将异常详细信息发送到我的服务器 每当我使用WebClient向WCF服务发送请求时,它都会发送请求,但不会执行回调事件,例如WebClient_UploadStringCompleted。我读到异常是在单独的线程中处理的 我已经尝试过但没有成功的: 螺纹起点 线 App.Curr

我需要处理App.xaml.cs文件的Application_UnhandledException事件中的异常。我使用e.ExceptionObject获取异常对象。现在,我需要在WebClient的帮助下使用WCF服务将异常详细信息发送到我的服务器

每当我使用WebClient向WCF服务发送请求时,它都会发送请求,但不会执行回调事件,例如WebClient_UploadStringCompleted。我读到异常是在单独的线程中处理的

我已经尝试过但没有成功的:

  • 螺纹起点
  • 线
  • App.Current.RootVisual.Dispatcher.BeginInvoke
  • RootFrame.Dispatcher.BeginInvoke
  • System.Windows.Deployment.Current.Dispatcher.BeginInvoke
  • 部署.Current.Dispatcher.BeginInvoke
  • 谁能告诉我,如何将错误详细信息从应用程序\u UnhandledException发送到具有WebClient的WCF服务

    代码:

    //要在未处理的异常上执行的代码


    在MainPage.HandleException方法中,我使用WebClient对象向WCF服务发送异常详细信息。但是,WebClient(WebClient_UploadStringCompleted)的回调函数从未执行过。执行应用程序UnhandledException事件的线程似乎立即挂起。

    是的,这在设计上无法工作。CLR在卸载AppDomain之前关闭AppDomain时引发该事件。您不能期望任何线程或异步操作运行到完成。所有代码必须在事件处理程序退出时完成。确保使用同步方法,并避免同时使用线程。通常的Do-Not-block-the-UI代码没有任何意义,在引发事件时已经没有UI了


    这在Windows Phone上确实是个问题,它没有WebClient的同步方法,比如WebClient.UploadString()。您必须使用AutoResteEvent使UploadStringAsync()同步。上载调用后调用其WaitOne()方法,在回调中调用其Set()方法。

    您可以在windows phone中使用IsolatedStorage存储异常,当应用程序重新启动时,您可以使用异步调用发送错误描述。

    您可以发布一些代码吗,以显示您在异常处理程序中的操作。@Amresh Kumar添加了代码段。谢谢。我将尝试让您知道结果。我最终使用了此..在异常时进行同步WCF调用不起作用,可能会让用户等待很长时间,等待应用程序崩溃。。
    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); 
    }