WPF 和 WndProc 因 .NET 异常而挂起

WPF and WndProc hangs with .NET exception

我需要在 shutdown/logoff 时执行代码。当执行这些操作中的任何一个时,我都会收到代码为 0xe0434352 的应用程序错误。我做错了什么?

这是我的代码:

    private void Window_SourceInitialized(object sender, EventArgs e)
    {
        IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
        HwndSource src = HwndSource.FromHwnd(windowHandle);
        src.AddHook(new HwndSourceHook(WndProc));
    }

    private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == 0x0011)
        {
            EventLog.WriteEntry("WPF1", "finally!");
            //my code
        }
        return IntPtr.Zero;
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
        HwndSource src = HwndSource.FromHwnd(windowHandle);
        src.RemoveHook(new HwndSourceHook(this.WndProc));
    }

异常错误:http://pastebin.com/gaX0ekaP

没关系,需要管理员权限。