Marshal.GetExceptionPointers和DispatcherUnhandledException

时间:2017-09-16 06:45:05

标签: c# wpf pinvoke

美好的一天。有一个WPF application +转储程序(MiniDumpWriteDump)。在应用程序中发生UnhandledException时,有必要创建minidump然后放置应用程序。

App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
    {
      base.OnStartup(e);
      AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
      Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
    }

[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
public static extern Int32 GetCurrentWin32ThreadId();

private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
  var pointers = Marshal.GetExceptionPointers(); //everything is ok :)
  var thread = GetCurrentWin32ThreadId();
  handleAppException(e.ExceptionObject as Exception, thread, pointers);
}

private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
  var pointers = Marshal.GetExceptionPointers(); //here IntPtr.Zero :(
  var thread = GetCurrentWin32ThreadId();
  handleAppException(e.Exception as Exception, thread, pointers);
}

private void handleAppException(int threadId, IntPtr pointers)
{
  System.Threading.Tasks.Task.Factory.StartNew(() =>
  {
    MiniDump.CreateMiniDump("C:\\Users\\Public\\Documents\\dump.dmp", threadId, pointers);
  }).Wait();
}

如果发生与另一个线程关联的异常,例如:

  Thread t = new Thread(() =>
  {
    throw new Exception("Thread exception!");
  });
  t.Start();

然后CurrentDomain_UnhandledExceptionMarshal.GetExceptionPointers ();正确返回指针。 如果UI线程中发生异常,例如:

throw new Exception("UI thread");

然后执行Current_DispatcherUnhandledExceptionGetExceptionPointers返回IntPtr.Zero

问题: 如何在ExceptionPointers中获取UI例外情况?

1 个答案:

答案 0 :(得分:0)

找到解决方案

使用Expected: is <org.robolectric.shadows.ShadowBitmapDrawable@ffffffe0> but: was <android.graphics.drawable.BitmapDrawable@ffffffe0> Expected :is <org.robolectric.shadows.ShadowBitmapDrawable@ffffffe0> Actual :<android.graphics.drawable.BitmapDrawable@ffffffe0> 代替Current.Dispatcher.UnhandledExceptionFilter