C#捕獲windows關機事件,在系統關機前做一些自己想做的事,

來源:互聯網
上載者:User

C#捕獲windows關機事件,在系統關機前做一些自己想做的事,

C#捕獲windows關機事件,在系統關機前做一些自己想做的事;

有些時候我們可能想在Windows關機時記錄或處理一些事情,這裡提供幾種方法。

方法一:

        /// <summary>        /// 視窗過程的回呼函數        /// </summary>        /// <param name="m"></param>        protected override void WndProc(ref Message m)        {            switch (m.Msg)            {                //此訊息在OnFormClosing之前                case WindowsMessage.WM_QUERYENDSESSION:                    //MessageBox.Show("WndProc.WM_QUERYENDSESSION.我要阻止系統關閉!");                    //this.Close();                    //this.Dispose();                    //Application.Exit();                    m.Result = (IntPtr)1; //阻止Windows登出、關機或重啟                    break;                default:                    break;            }            base.WndProc(ref m);        }
方法二:
        protected override void OnFormClosing(FormClosingEventArgs e)        {            switch (e.CloseReason)            {                case CloseReason.ApplicationExitCall:                    e.Cancel = true;                    MessageBox.Show("攔截關閉要求事件!");                    break;                case CloseReason.FormOwnerClosing:                    e.Cancel = true;                    MessageBox.Show("攔截自身關閉事件!");                    break;                case CloseReason.MdiFormClosing:                    e.Cancel = true;                    MessageBox.Show("攔截MDI表單關閉事件!");                    break;                case CloseReason.None:                    break;                case CloseReason.TaskManagerClosing:                    e.Cancel = true;                    MessageBox.Show("攔截工作管理員關閉事件!");                    break;                case CloseReason.UserClosing:                                        //登出或關機會觸發此事件;                    //MessageBox.Show("攔截使用者關閉事件!");                    e.Cancel = false;                    break;                case CloseReason.WindowsShutDown:                    e.Cancel = true;                    MessageBox.Show("攔截關機事件!");                    break;                default:                    break;            }            base.OnFormClosing(e);        }
方法三:

//當使用者試圖登出或關閉系統時發生。              SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);//下面是系統登出或關閉事件處理常式,          private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)        {            if (MessageBox.Show(this, "是否允許系統登出!", "系統提示", MessageBoxButtons.YesNo) != DialogResult.Yes)            {                e.Cancel = true;            }            else            {                e.Cancel = false;            }            SessionEndReasons reason = e.Reason;            switch (reason)            {                case SessionEndReasons.Logoff:                    MessageBox.Show("使用者正在登出。作業系統繼續運行,但啟動此應用程式的使用者正在登出。");                    break;                case SessionEndReasons.SystemShutdown:                    MessageBox.Show("作業系統正在關閉。");                    break;            }        }        //如果把上面的事件處理常式修改成如下          //private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)          //       {          //          e.Cancel = true;         //   }         //那會出現什麼情況,你點擊開始菜單關機選擇登出、關機、或重新啟動將會失效,電腦不能順利關機了,進一步的話把程式做成Windows服務,暈,惡作劇?         //SessionEnded事件同上,事件參數類為SessionEndedEventArgs,同SessionEndingEventArgs相比少了Cancel屬性,Cancel屬性同一些windows下的某些事件差不多,比如Form.Closing事件,Control.Validating事件。        //補充,如果需要擷取應用程式需要的系統資訊,可以訪問System.Windows.Forms.SystemInformation類,這也是一個很有用的類,它提供了一組靜態屬性。
但在調試時並沒有執行到這裡!


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.