I have an issue with my WPF application crashing randomly (but at least twice per day) leaving the following message in windows Application log:
Application: AppName.exe Framework Version: v4.0.30319 Description: The process was terminated due to an internal error in the .NET Runtime at IP xxxxxxxx (xxxxxxxx) with exit code 80131506.
Just to give you the background, this application runs on an embedded system running Windows Embedded Standard 2009, and together with another process is the only application running on the device (even explorer is disabled since it is not needed).
After some trial-and-error investigation, I have isolated the code triggering the error. It is a hook installed on the mainwindow and used to intercept the HWND messages to know when the monitor goes off or in standby mode. Since the system is equipped with a touch panel, I cover my application mainwindow with a panel while the monitor is off so when the user touches the monitor to take it out of standby mode, it won't erroneously click one of the buttons in my mainwindow. When the panel itself gets a "click" event, it closes, disappearing and thus allowing the user to resume normal operativity.
here is how i instantiate the hook and the function that gets called when the hwnd is intercepted:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowInteropHelper helper = new WindowInteropHelper(this);
HwndSource.FromHwnd(helper.Handle).AddHook(HwndSourceHookHandler);
}
private IntPtr HwndSourceHookHandler(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
handled = false;
if (msg == WM_SYSCOMMAND && wParam == (IntPtr)SC_MONITORPOWER)
{
if (lParam == (IntPtr)MONITOR_OFF || lParam == (IntPtr)MONITOR_STANDBY)
{
AppName.Shell.canvasStandBy.Visibility = System.Windows.Visibility.Visible;
}
}
return IntPtr.Zero;
}
If I comment out the code in the Window_Loaded bit the crash does not happen anymore... Can you point out what is wrong with this code or give me a hint for another way to make it so that the users' clicks on the monitor while it is off do not get to the underlying mainwindow?
Thanks in advance for your help :)