C#制作Windows service服务系列三--制作可控制界面的windows服务(windows service)
2010-09-30 21:04:29 来源:WEB开发网第三步:添加用到的方法,代码如下:
Methods#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系列学习心得。呵呵
更多精彩
赞助商链接