在前面兩篇文章中,我們可以做到自啟動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系列學習心得。呵呵