private void ForceUnsunscribeFromForegroundChangedEvent()
 {
     if (s_ForegroundChangedHookHandle != 0)
     {
         bool result = WinApi.UnhookWinEvent(s_ForegroundChangedHookHandle);
         s_ForegroundChangedHookHandle = 0;
         s_ForegroundChangedDelegate = null;
         if (result == false)
         {
             int errorCode = Marshal.GetLastWin32Error();
             throw new Win32Exception(errorCode);
         }
     }
 }
        private void EnsureSubscribeToForegroundChangedEvent()
        {
            if (s_ForegroundChangedHookHandle == 0)
            {
                s_ForegroundChangedDelegate = ForegroundChangedHookProc;
                s_ForegroundChangedHookHandle = WinApi.SetWinEventHook(EventConstants.EVENT_SYSTEM_FOREGROUND, EventConstants.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, s_ForegroundChangedDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);

                if (s_ForegroundChangedHookHandle == 0)
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    throw new Win32Exception(errorCode);
                }
            }
        }