C#製作Windows service服務系列三–製作可控制介面的windows服務(windows service)

來源:互聯網
上載者:User

在前面兩篇文章中,我們可以做到自啟動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完全可以做到這些。這裡只是作了一個簡單示範。稍後可能會寫WCF系列學習心得。呵呵

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.