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

原文:http://blog.csdn.net/downmoon/article/details/4771990

在前面两篇文章中,我们可以做到自启动windows和定时自动执行windows service

  系列一: 制作一个可安装、可启动、可停止、可卸载的windows service (downmoon 原创)

  系列二:演示一个定期执行的windows服务及调试(downmoon )

  系列三:windows service系列三--制作可控制界面的windows service

  本文将重点介绍下如何通过界面控制肉眼看不到的windows service,其实主要用到的类是:

  ServiceController ,可以看官方 的说明,如果急于看到结果,请直接往下看。

  准备工作:假定我们已经做好一个wondows service,并且已经部署,服务名为pingServiceDemo,
  
  下面我们制作一个可控制的界面来操作这个windows service

   开始:

  第一步:新建一个winform项目,名为 CtrlPingService,添加System.ServiceProcess.dll的引用。

  

  第二步:定义变量,添加按钮事件,主要代码如下:

  

[c-sharp] view plain copy print ?
  1. private System.Windows.Forms.Button btnStart;  
  2. private System.Windows.Forms.Button btnStop;  
  3. private System.Windows.Forms.Button btnExit;  
  4. private System.ServiceProcess.ServiceController sController;  
  5. private Label lbInfo;  
  6. private Button ftnPause;  
  7. private Button btnResume;  
  8. private Label lbServiceName;  
  9. private TextBox txtServerName;  
  10. private Button btnServiceState;  
  11. private string serverName;  
  12.  Ecents  
  13. private void frmMain_Load(object sender, EventArgs e)  
  14. {  
  15.     if (serverName == null) { serverName = txtServerName.Text.Trim(); }  
  16.     lbInfo.Text = "当前服务状态是:" + getStateForService(serverName);  
  17. }  
  18. private void btnServiceState_Click(object sender, EventArgs e)  
  19. {  
  20.     lbInfo.Text = "当前服务状态是:" + getStateForService(serverName);  
  21. }  
  22. private void btnStart_Click(object sender, System.EventArgs e)  
  23. {  
  24.     runService(serverName);  
  25. }  
  26. private void btnStop_Click(object sender, System.EventArgs e)  
  27. {  
  28.     killService(serverName);  
  29. }  
  30. private void ftnPause_Click(object sender, EventArgs e)  
  31. {  
  32.     pauseService(serverName);  
  33. }  
  34. private void btnResume_Click(object sender, EventArgs e)  
  35. {  
  36.     resumeService(serverName);  
  37. }  
  38. private void btnExit_Click(object sender, System.EventArgs e)  
  39. {  
  40.     this.Close();  
  41.     Application.Exit();  
  42. }  
  43. #endregion  
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

 

  第三步:添加用到的方法,代码如下:

  

[c-sharp] view plain copy print ?
  1. #region Methods   
  2.        private void runService(string servername)  
  3.        {  
  4.            ServiceController sc = new ServiceController(servername);  
  5.            ServiceControllerStatus st = sc.Status;  
  6.            switch (st)  
  7.            {  
  8.                case ServiceControllerStatus.StopPending:  
  9.                case ServiceControllerStatus.Stopped:  
  10.                    sc.Start();  
  11.                    break;  
  12.                defaultbreak;  
  13.            }  
  14.            sc.WaitForStatus(ServiceControllerStatus.Running);  
  15.            st = sc.Status;//再次获取服务状态   
  16.            if (st == ServiceControllerStatus.Running)  
  17.            {  
  18.                lbInfo.Text = "服务  " + sc.ServiceName + "  已经启动!";  
  19.            }  
  20.        }  
  21.        private void killService(string servername)  
  22.        {  
  23.            ServiceController sc = new ServiceController(servername);  
  24.            ServiceControllerStatus st = sc.Status;  
  25.            switch (st)  
  26.            {  
  27.                case ServiceControllerStatus.Running:  
  28.                case ServiceControllerStatus.StartPending:  
  29.                case ServiceControllerStatus.Paused:  
  30.                case ServiceControllerStatus.PausePending:  
  31.                case ServiceControllerStatus.ContinuePending:  
  32.                    sc.Stop();  
  33.                    sc.WaitForStatus(ServiceControllerStatus.Stopped);  
  34.                    break;  
  35.            }  
  36.            st = sc.Status;//再次获取服务状态   
  37.            if (st == ServiceControllerStatus.Stopped)  
  38.            {  
  39.                lbInfo.Text = "服务 " + sc.ServiceName + "  已经停止!";  
  40.            }  
  41.        }  
  42.        private void pauseService(string servername)  
  43.        {  
  44.            ServiceController sc = new ServiceController(servername);  
  45.            ServiceControllerStatus st = sc.Status;  
  46.            switch (st)  
  47.            {  
  48.                case ServiceControllerStatus.Running:  
  49.                case ServiceControllerStatus.StartPending:  
  50.                    sc.Pause();  
  51.                    sc.WaitForStatus(ServiceControllerStatus.Paused);  
  52.                    break;  
  53.            }  
  54.            st = sc.Status;//再次获取服务状态   
  55.            if (st == ServiceControllerStatus.Paused)  
  56.            {  
  57.                lbInfo.Text = "服务 " + sc.ServiceName + "  已经暂停!";  
  58.            }  
  59.        }  
  60.        private void resumeService(string servername)  
  61.        {  
  62.            ServiceController sc = new ServiceController(servername);  
  63.            ServiceControllerStatus st = sc.Status;  
  64.            switch (st)  
  65.            {  
  66.                case ServiceControllerStatus.Paused:  
  67.                case ServiceControllerStatus.PausePending:  
  68.                    sc.Continue();  
  69.                    sc.WaitForStatus(ServiceControllerStatus.Running);  
  70.                    break;  
  71.            }  
  72.            st = sc.Status;//再次获取服务状态   
  73.            if (st == ServiceControllerStatus.Running)  
  74.            {  
  75.                lbInfo.Text = "服务 " + sc.ServiceName + "  已经继续!";  
  76.            }  
  77.        }  
  78.        private string getStateForService(string servername)  
  79.        {  
  80.            ServiceController sc = new ServiceController(servername);  
  81.            ServiceControllerStatus st = sc.Status;  
  82.            return sc.Status.ToString();  
  83.        }  
  84.        #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系列学习心得。呵呵

  助人等于自助!   [email protected]

你可能感兴趣的:(C#制作Windows service服务系列三--制作可控制界面的Windows服务(windows service))