static FlashWindowExListener() { int processId = PI.GetCurrentThreadId(); // create an instance of the delegate that // won't be garbage collected to avoid: // Managed Debugging Assistant 'CallbackOnCollectedDelegate' :** // 'A callback was made on a garbage collected delegate of type // 'WpfApp1!WpfApp1.MainWindow+NativeMethods+CBTProc::Invoke'. // This may cause application crashes, corruption and data loss. // When passing delegates to unmanaged code, they must be // kept alive by the managed application until it is guaranteed // that they will never be called.' _hookProc = ShellProc; // we are interested in listening to WH_SHELL events, mainly the HSHELL_REDRAW event. _hHook = PI.SetWindowsHookEx(PI.WH_.SHELL, _hookProc, IntPtr.Zero, processId); Application.ApplicationExit += delegate { PI.UnhookWindowsHookEx(_hHook); }; }