首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 開發 Windows Service 程序(下):服務停止控制、狀態定時器與系統設定對話框

     編輯:關於C語言

停止服務按鈕實作


private void btnStopService_Click(object sender, EventArgs e) {
    if (!bolServiceInstalled) return;
    using (ServiceController ctrl = new ServiceController("MyFileSystemWatcher")) {
        if (ctrl.Status == ServiceControllerStatus.Running) ctrl.Stop();
    }
}

Stop() 為非同步通知,不等待服務實際停止即返回。

定時器同步按鈕狀態

每 2 秒檢查服務狀態,啟用對應按鈕:


private void myTimer_Tick(object sender, EventArgs e) {
    if (!bolServiceInstalled) return;
    using (ServiceController ctrl = new ServiceController("MyFileSystemWatcher")) {
        btnStartService.Enabled = (ctrl.Status == ServiceControllerStatus.Stopped);
        btnStopService.Enabled = (ctrl.Status == ServiceControllerStatus.Running);
    }
}

系統設定對話框

點擊工具列「系統設定」按鈕顯示:

對話框繫結 MyConfig 物件屬性,修改後需重啟服務生效。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved