成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#啟動Windows服務及關閉實例實現

開發 后端
C#啟動Windows服務及關閉實例實現是如何做到的呢?那么本文就向介紹C#啟動Windows服務及關閉實例實現的具體的代碼。

C#啟動Windows服務及關閉實例實現的步驟和使用調用的類是什么呢?讓我們來看看具體的實現:

C# 控制Windows服務的啟動與關閉

  1. // ......  
  2.   using System.ServiceProcess;  
  3.  // ......  
  4.  /// <summary>  
  5. /// Restart windows service  
  6. /// </summary>  
  7. /// <param name="serviceName">  
  8. ///the windows service display name</param>  
  9. /// <returns> If the restart successfully  
  10. ///return true else return false</returns>  
  11. public static bool RestartWindowsService(string serviceName)  
  12. {  
  13.     bool bResult = false;  
  14.     try 
  15.     {  
  16. try 
  17. {  //C#啟動Windows服務及關閉
  18.     StopWindowsService(serviceName);  
  19.     Thread.Sleep(1000);  
  20. }  
  21. catch (Exception ex)  
  22. {  
  23.     StartWindowsService(serviceName);  
  24.     Thread.Sleep(1000);  
  25.     StopWindowsService(serviceName);  
  26.     Thread.Sleep(1000);  
  27.     Console.WriteLine(ex.Message);  
  28. }  
  29. try 
  30. {  
  31.     StartWindowsService(serviceName);  
  32.     Thread.Sleep(1000);  
  33. }  
  34. catch (Exception ex)  //C#啟動Windows服務及關閉
  35. {  
  36.     StopWindowsService(serviceName);  
  37.     Thread.Sleep(1000);  
  38.     StartWindowsService(serviceName);  
  39.     Thread.Sleep(1000);  
  40.     Console.WriteLine(ex.Message);  
  41. }  
  42. bResult = true;  
  43.     }  
  44.     catch (Exception ex)  
  45.     {  
  46. bResult = false;  
  47. throw ex;  
  48.     }  
  49.     return bResult;  
  50. }  
  51.  //C#啟動Windows服務及關閉
  52. /// <summary>  
  53. /// Start windows service  
  54. /// </summary>  
  55. /// <param name="serviceName">  
  56. ///the windows service display name</param>  
  57. /// <returns>If the start successfully   
  58. ///return true else return false</returns>  
  59. public static bool StopWindowsService(string serviceName)  
  60. {  
  61.     ServiceController[] scs = ServiceController.GetServices();  
  62.     bool bResult = false;  
  63.     foreach (ServiceController sc in scs)  
  64.     {  
  65. if (sc.DisplayName == serviceName)  
  66. {  
  67.     try 
  68.     {  
  69. sc.WaitForStatus(ServiceControllerStatus.Running,   
  70. TimeSpan.FromSeconds(30));  
  71. sc.Stop();  
  72. bResult = true;  
  73.     }  
  74.     catch (Exception ex)  
  75.     {  
  76. bResult = false;  
  77. throw ex;  
  78.     }  
  79. }  
  80.     }  
  81.     return bResult;  
  82. }  
  83.  //C#啟動Windows服務及關閉
  84. /// <summary>  
  85. /// Stop windows service  
  86. /// </summary>  
  87. /// <param name="serviceName">the windows  
  88. ///service display name</param>  
  89. /// <returns>If the stop successfully   
  90. ///return true else return false</returns>  
  91. public static bool StartWindowsService(  
  92. string serviceName)  
  93. {  
  94.     ServiceController[] scs  
  95.  = ServiceController.GetServices();  
  96.     bool bResult = false;  
  97.     foreach (ServiceController sc in scs)  
  98.     {  
  99. if (sc.DisplayName == serviceName)  
  100. {  
  101.     try 
  102.     {  
  103. sc.WaitForStatus(ServiceControllerStatus.Stopped,   
  104. TimeSpan.FromSeconds(30));  
  105. sc.Start();  
  106. bResult = true;  
  107.     }  
  108.     catch (Exception ex)  
  109.     {  
  110. bResult = false;  
  111. throw ex;  
  112.     }  
  113. }  //C#啟動Windows服務及關閉
  114.     }  
  115.     return bResult;  
C#啟動Windows服務及關閉具體實現就向你介紹到這里,希望對你了解和學習C#啟動Windows服務及關閉有所幫助。

【編輯推薦】

  1. C#Windows服務程序安裝淺析
  2. C#Windows服務程序開發的體會
  3. C#啟動windows服務的方法淺析
  4. C#windows服務狀態改變操作淺析
  5. C#Windows服務程序開發實例介紹
責任編輯:仲衡 來源: 百度空間
相關推薦

2009-08-14 17:04:19

Windows后臺服務

2009-08-14 16:02:50

C#啟動windows

2009-08-24 10:37:27

C# 泛型

2009-08-14 16:41:22

C#啟動Windows

2009-09-07 05:50:59

C# Timer用法

2009-03-27 10:10:13

c#遠程啟動遠程管理

2009-09-01 18:29:10

C#繼承C#多態

2009-08-07 16:43:44

C#調用Windows

2009-08-25 15:38:12

C# Windows服

2009-08-27 13:05:06

C#接口特點C#接口實例

2009-08-31 16:37:20

C#接口定義

2009-08-17 09:39:40

C# Windows

2009-08-31 17:16:12

C#實現接口

2009-08-21 10:13:02

C#異步初步

2009-08-26 09:22:44

C#實現打印功能

2009-08-26 11:07:36

C#打印窗體

2009-08-26 11:32:37

C#打印文檔

2009-09-02 17:28:26

C#程序設計Windows窗體

2009-12-24 09:16:11

C#泛型

2009-08-14 11:00:16

C#創建Windows
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩久久中文字幕 | 精品国产欧美一区二区三区不卡 | 欧美激情一区二区三区 | 欧美成人激情视频 | 99精品国产一区二区青青牛奶 | 日韩不卡视频在线观看 | 国产激情网站 | 色婷婷亚洲一区二区三区 | 精品国产一区二区国模嫣然 | 日韩精品一区二区三区 | 久久精品国产免费一区二区三区 | 一区二区精品电影 | 国产综合久久久久久鬼色 | 久久骚 | 欧美一级三级 | 一区二区在线免费观看视频 | 国产日韩精品久久 | 逼逼网| 日韩精品一区二区在线观看 | 中文字幕一区在线 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | av中文字幕在线播放 | 久久久久久久久久久成人 | 在线免费视频一区 | 精品伦精品一区二区三区视频 | 久久久国产视频 | 午夜国产精品视频 | 综合一区二区三区 | 国产精品99999999 | 久久久爽爽爽美女图片 | 亚洲激情网站 | 久久综合九色综合欧美狠狠 | 日韩一区二区三区在线观看 | 国产一区二区 | 成人网av| 国产精品永久免费视频 | 91极品视频| 99免费视频 | 日韩欧美精品一区 | 奇米久久 | 亚洲一二三区免费 |