C#製作可控制介面的windows服務

flynike發表於2021-09-09

在前面兩篇文章中,我們可以做到自啟動windows和定時自動執行windows service

  系列一: 製作一個可安裝、可啟動、可停止、可解除安裝的Windows service(downmoon原創)

  系列二:演示一個定期執行的windows服務及除錯(windows service)(downmoon)

  系列三: windows service系列三--製作可控制介面的windows service

  本文將重點介紹下如何透過介面控制肉眼看不到的windows service。其實主要用到的類是:

  ServiceController ,可以看官方 的說明,如果急於看到結果,請直接往下看。

  準備工作:假定我們已經做好一個wondows service,並且已經部署,服務名為pingServiceDemo,
  
  下面我們製作一個可控制的介面來操作這個windows service

  開始:

  第一步:新建一個winform專案,名為 CtrlPingService,新增System.ServiceProcess.dll的引用。

       第二步:定義變數,新增按鈕事件,主要程式碼如下:

     private System.Windows.Forms.Button btnStart;
        private System.Windows.Forms.Button btnStop;
        private System.Windows.Forms.Button btnExit;
        private System.ServiceProcess.ServiceController sController;
        private Label lbInfo;
        private Button ftnPause;
        private Button btnResume;
        private Label lbServiceName;
        private TextBox txtServerName;
        private Button btnServiceState;
        private string serverName;
 
  #region Ecents

        private void frmMain_Load(object sender, EventArgs e)
        {
            if (serverName == null) { serverName = txtServerName.Text.Trim(); }
            lbInfo.Text = "當前服務狀態是:" + getStateForService(serverName);
        }
        private void btnServiceState_Click(object sender, EventArgs e)
        {
            lbInfo.Text = "當前服務狀態是:" + getStateForService(serverName);
        }
        private void btnStart_Click(object sender, System.EventArgs e)
        {
            runService(serverName);
        }
        private void btnStop_Click(object sender, System.EventArgs e)
        {
            killService(serverName);
        }
        private void ftnPause_Click(object sender, EventArgs e)
        {
            pauseService(serverName);
        }
        private void btnResume_Click(object sender, EventArgs e)
        {
            resumeService(serverName);
        }
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
 #endregion


第三步:新增用到的方法,程式碼如下:   


  #region Methods

        private void runService(string servername)
        {
            ServiceController sc = new ServiceController(servername);
            ServiceControllerStatus st = sc.Status;
            switch (st)
            {
                case ServiceControllerStatus.StopPending:
                case ServiceControllerStatus.Stopped:
                    sc.Start();
                    break;
                default: break;
            }
            sc.WaitForStatus(ServiceControllerStatus.Running);
            st = sc.Status;//再次獲取服務狀態
            if (st == ServiceControllerStatus.Running)
            {
                lbInfo.Text = "服務  " + sc.ServiceName + "  已經啟動!";
            }
        }
        private void killService(string servername)
        {
            ServiceController sc = new ServiceController(servername);
            ServiceControllerStatus st = sc.Status;
            switch (st)
            {
                case ServiceControllerStatus.Running:
                case ServiceControllerStatus.StartPending:
                case ServiceControllerStatus.Paused:
                case ServiceControllerStatus.PausePending:
                case ServiceControllerStatus.ContinuePending:
                    sc.Stop();
                    sc.WaitForStatus(ServiceControllerStatus.Stopped);
                    break;
            }
            st = sc.Status;//再次獲取服務狀態
            if (st == ServiceControllerStatus.Stopped)
            {
                lbInfo.Text = "服務 " + sc.ServiceName + "  已經停止!";
            }
        }
        private void pauseService(string servername)
        {
            ServiceController sc = new ServiceController(servername);
            ServiceControllerStatus st = sc.Status;
            switch (st)
            {
                case ServiceControllerStatus.Running:
                case ServiceControllerStatus.StartPending:
                    sc.Pause();
                    sc.WaitForStatus(ServiceControllerStatus.Paused);
                    break;
            }
            st = sc.Status;//再次獲取服務狀態
            if (st == ServiceControllerStatus.Paused)
            {
                lbInfo.Text = "服務 " + sc.ServiceName + "  已經暫停!";
            }
        }
        private void resumeService(string servername)
        {
            ServiceController sc = new ServiceController(servername);
            ServiceControllerStatus st = sc.Status;
            switch (st)
            {
                case ServiceControllerStatus.Paused:
                case ServiceControllerStatus.PausePending:
                    sc.Continue();
                    sc.WaitForStatus(ServiceControllerStatus.Running);
                    break;
            }
            st = sc.Status;//再次獲取服務狀態
            if (st == ServiceControllerStatus.Running)
            {
                lbInfo.Text = "服務 " + sc.ServiceName + "  已經繼續!";
            }
        }
        private string getStateForService(string servername)
        {
            ServiceController sc = new ServiceController(servername);
            ServiceControllerStatus st = sc.Status;
            return sc.Status.ToString();
        }
  #endregion

     OK! 最後欣賞下我們的成果:

     總結:其實WCF完全可以做到這些。這裡只是作了一個簡單演示。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2618/viewspace-2800497/,如需轉載,請註明出處,否則將追究法律責任。

相關文章