protected override void WndProc(ref Message m)
{
if (m.Msg == WM_QUERYENDSESSION)
sessionEndInProgress = true;
if ((m.Msg == WM_ENDSESSION) && ((int) m.WParam == 0)) // if "session end" was canceled (by some other reasons)
sessionEndInProgress = false;
base.WndProc (ref m);
}