C#制作Windows service服务系列三--制作可控制界面的windows服务(windows service)

在前面两篇文章中,我们可以做到自启动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系列学习心得。呵呵

posted @ 2009-11-05 16:16  邀月  阅读(10073)  评论(13编辑  收藏  举报