C#制作Windows service服务系列三--制作可控制界面的windows服务(windows service)
2010-09-30 21:04:29 来源:WEB开发网本文将重点介绍下如何通过界面控制肉眼看不到的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;
Ecents#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
更多精彩
赞助商链接