WPF和WndProc挂起.NET异常

时间:2015-09-06 16:24:47

标签: wpf exception wndproc

我需要在关机/注销时执行代码。当任何这些操作发生时,我得到一个代码为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

1 个答案:

答案 0 :(得分:0)

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