自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#啟動(dòng)Windows服務(wù)及關(guān)閉實(shí)例實(shí)現(xiàn)

開發(fā) 后端
C#啟動(dòng)Windows服務(wù)及關(guān)閉實(shí)例實(shí)現(xiàn)是如何做到的呢?那么本文就向介紹C#啟動(dòng)Windows服務(wù)及關(guān)閉實(shí)例實(shí)現(xiàn)的具體的代碼。

C#啟動(dòng)Windows服務(wù)及關(guān)閉實(shí)例實(shí)現(xiàn)的步驟和使用調(diào)用的類是什么呢?讓我們來看看具體的實(shí)現(xiàn):

C# 控制Windows服務(wù)的啟動(dòng)與關(guān)閉

  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#啟動(dòng)Windows服務(wù)及關(guān)閉
  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#啟動(dòng)Windows服務(wù)及關(guān)閉
  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#啟動(dòng)Windows服務(wù)及關(guān)閉
  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#啟動(dòng)Windows服務(wù)及關(guān)閉
  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#啟動(dòng)Windows服務(wù)及關(guān)閉
  114.     }  
  115.     return bResult;  
C#啟動(dòng)Windows服務(wù)及關(guān)閉具體實(shí)現(xiàn)就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#啟動(dòng)Windows服務(wù)及關(guān)閉有所幫助。

【編輯推薦】

  1. C#Windows服務(wù)程序安裝淺析
  2. C#Windows服務(wù)程序開發(fā)的體會(huì)
  3. C#啟動(dòng)windows服務(wù)的方法淺析
  4. C#windows服務(wù)狀態(tài)改變操作淺析
  5. C#Windows服務(wù)程序開發(fā)實(shí)例介紹
責(zé)任編輯:仲衡 來源: 百度空間
相關(guān)推薦

2009-08-14 17:04:19

Windows后臺(tái)服務(wù)

2009-08-14 16:02:50

C#啟動(dòng)windows

2009-08-24 10:37:27

C# 泛型

2009-08-14 16:41:22

C#啟動(dòng)Windows

2009-09-07 05:50:59

C# Timer用法

2009-03-27 10:10:13

c#遠(yuǎn)程啟動(dòng)遠(yuǎn)程管理

2009-09-01 18:29:10

C#繼承C#多態(tài)

2009-08-27 13:05:06

C#接口特點(diǎn)C#接口實(shí)例

2009-08-31 16:37:20

C#接口定義

2009-08-31 17:16:12

C#實(shí)現(xiàn)接口

2009-08-25 15:38:12

C# Windows服

2009-08-17 09:39:40

C# Windows

2009-08-21 10:13:02

C#異步初步

2009-08-26 09:22:44

C#實(shí)現(xiàn)打印功能

2009-08-26 11:07:36

C#打印窗體

2009-08-26 11:32:37

C#打印文檔

2009-08-07 16:43:44

C#調(diào)用Windows

2009-09-02 17:28:26

C#程序設(shè)計(jì)Windows窗體

2009-08-26 14:03:26

C#打印原理

2009-12-24 09:16:11

C#泛型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)