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

C#啟動(dòng)Windows服務(wù)的窗體程序淺析

開發(fā) 后端
C#啟動(dòng)Windows服務(wù)的窗體程序主要向你介紹了C#隨機(jī)啟動(dòng)、監(jiān)控WINDOWS服務(wù)的窗體程序,那么C#啟動(dòng)Windows服務(wù)的窗體程序的思路和代碼實(shí)現(xiàn)如何呢?那么本文就向你一一介紹。

C#啟動(dòng)Windows服務(wù)的窗體程序的由來(lái):最近應(yīng)客戶的要求,做了一個(gè)定時(shí)監(jiān)控WINDOWS服務(wù)的程序,要做到隨機(jī)啟動(dòng),定時(shí)監(jiān)控指定的服務(wù),如果沒有開啟則開啟,由于時(shí)間倉(cāng)促,沒有經(jīng)過(guò)長(zhǎng)時(shí)間的服務(wù)器端運(yùn)行,現(xiàn)將思路及代碼公布,以后有改進(jìn)會(huì)及時(shí)更新:

一、C#啟動(dòng)Windows服務(wù)的窗體程序思路:

本程序的核心在于隨機(jī)啟動(dòng)和WINDOWS服務(wù)上,對(duì)于隨機(jī)啟動(dòng),引入Microsoft.Win32命名空間,利用RegistryKey類即可完成對(duì)注冊(cè)表的增、刪、改等操作;對(duì)于WINDOWS服務(wù),引入System.ServiceProcess命名空間,利用ServiceController類即可完成對(duì)系統(tǒng)服務(wù)的啟動(dòng)、停止、查詢等操作。改日就測(cè)試程序的穩(wěn)定性及資源消耗率等指標(biāo)。

二、C#啟動(dòng)Windows服務(wù)的窗體程序代碼如下,這里程序默認(rèn)為開啟MSSQLSERVER服務(wù),并添加了托盤區(qū)圖標(biāo),可以在啟動(dòng)時(shí)或啟動(dòng)后最小化到托盤區(qū):

  1. using System;  //C#啟動(dòng)Windows服務(wù)的窗體程序
  2. using System.Drawing;  
  3. using System.Collections;  
  4. using System.ComponentModel;  
  5. using System.windows.Forms;  
  6. using System.Data;  
  7. using System.ServiceProcess;  
  8. using System.IO;  
  9. using Microsoft.Win32;  
  10. namespace WatchService  
  11. {  
  12. /// ﹤summary﹥  
  13. /// Form1 的摘要說(shuō)明,C#啟動(dòng)Windows服務(wù)的窗體程序  
  14. /// ﹤/summary﹥  
  15. public class WatchService : System.windows.Forms.Form  
  16. {  
  17. private System.windows.Forms.Button btn_startWatch;  
  18. private System.windows.Forms.Button btn_stopWatch;  
  19. private System.windows.Forms.Button btn_startReg;  
  20. private System.windows.Forms.Button btn_stopReg;  
  21. private System.windows.Forms.Label lbl_appStatus;  
  22. private System.windows.Forms.TextBox tbx_serviceName;  
  23. private System.windows.Forms.Button btn_Exit;  
  24. private System.windows.Forms.TextBox tbx_interval;  
  25. private System.windows.Forms.Timer timer1;  
  26. private System.windows.Forms.NotifyIcon notifyIcon1;  
  27. private System.ComponentModel.IContainer components;  
  28. public WatchService()  
  29. {  
  30. // C#啟動(dòng)Windows服務(wù)的窗體程序 
  31. // windows 窗體設(shè)計(jì)器支持所必需的  
  32. //  
  33. InitializeComponent();  
  34. //  
  35. // TODO: 在 InitializeComponent   
  36. //調(diào)用后添加任何構(gòu)造函數(shù)代碼  
  37. //  
  38. }  
  39. /// ﹤summary﹥
  40. ///C#啟動(dòng)Windows服務(wù)的窗體程序  
  41. /// 清理所有正在使用的資源。  
  42. /// ﹤/summary﹥  
  43. protected override void Dispose( bool disposing )  
  44. {  
  45. if( disposing )  
  46. {  
  47. if (components != null)   
  48. {  
  49. components.Dispose();  
  50. }  
  51. }  
  52. base.Dispose( disposing );  
  53. }  
  54. #region windows 窗體設(shè)計(jì)器生成的代碼  
  55. /// ﹤summary﹥  
  56. /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改  
  57. /// 此方法的內(nèi)容。  
  58. /// ﹤/summary﹥  
  59. private void InitializeComponent()  
  60. {  
  61. this.components = new System.ComponentModel.Container();  
  62. System.Resources.ResourceManager resources = new 
  63.  System.Resources.ResourceManager(typeof(WatchService));  
  64. this.btn_startWatch = new System.windows.Forms.Button();  
  65. this.btn_stopWatch = new System.windows.Forms.Button();  
  66. this.btn_startReg = new System.windows.Forms.Button();  
  67. this.btn_stopReg = new System.windows.Forms.Button();  
  68. this.lbl_appStatus = new System.windows.Forms.Label();  
  69. this.tbx_serviceName = new System.windows.Forms.TextBox();  
  70. this.btn_Exit = new System.windows.Forms.Button();  
  71. this.tbx_interval = new System.windows.Forms.TextBox();  
  72. this.timer1 = new System.windows.Forms.Timer(this.components);  
  73. this.notifyIcon1 = new   
  74. System.windows.Forms.NotifyIcon(this.components);  
  75. this.SuspendLayout();  
  76. //   
  77. // btn_startWatch  
  78. //   C#啟動(dòng)Windows服務(wù)的窗體程序
  79. this.btn_startWatch.Location = new System.Drawing.Point(112, 8);  
  80. this.btn_startWatch.Name = "btn_startWatch";  
  81. this.btn_startWatch.Size = new System.Drawing.Size(64, 23);  
  82. this.btn_startWatch.TabIndex = 0;  
  83. this.btn_startWatch.Text = "開始監(jiān)控";  
  84. this.btn_startWatch.Click +=   
  85. new System.EventHandler(this.btn_startWatch_Click);  
  86. //   
  87. // btn_stopWatch  
  88. //   
  89. this.btn_stopWatch.Location = new System.Drawing.Point(184, 8);  
  90. this.btn_stopWatch.Name = "btn_stopWatch";  
  91. this.btn_stopWatch.Size = new System.Drawing.Size(64, 23);  
  92. this.btn_stopWatch.TabIndex = 1;  
  93. this.btn_stopWatch.Text = "停止監(jiān)控";  
  94. //   
  95. // btn_startReg  
  96. //   C#啟動(dòng)Windows服務(wù)的窗體程序
  97. this.btn_startReg.Location = new System.Drawing.Point(112, 40);  
  98. this.btn_startReg.Name = "btn_startReg";  
  99. this.btn_startReg.Size = new System.Drawing.Size(88, 24);  
  100. this.btn_startReg.TabIndex = 2;  
  101. this.btn_startReg.Text = "開啟隨機(jī)啟動(dòng)";  
  102. this.btn_startReg.Click += new   
  103. System.EventHandler(this.btn_startReg_Click);  
  104. //   
  105. // btn_stopReg  
  106. //   
  107. this.btn_stopReg.Location = new System.Drawing.Point(232, 40);  
  108. this.btn_stopReg.Name = "btn_stopReg";  
  109. this.btn_stopReg.Size = new System.Drawing.Size(88, 24);  
  110. this.btn_stopReg.TabIndex = 3;  
  111. this.btn_stopReg.Text = "關(guān)閉隨機(jī)啟動(dòng)";  
  112. this.btn_stopReg.Click += new   
  113. System.EventHandler(this.btn_stopReg_Click);  
  114. //   
  115. // lbl_appStatus  
  116. //   C#啟動(dòng)Windows服務(wù)的窗體程序
  117. this.lbl_appStatus.ForeColor = System.Drawing.Color.Red;  
  118. this.lbl_appStatus.Location = new System.Drawing.Point(16, 72);  
  119. this.lbl_appStatus.Name = "lbl_appStatus";  
  120. this.lbl_appStatus.Size = new System.Drawing.Size(304, 23);  
  121. this.lbl_appStatus.TabIndex = 4;  
  122. this.lbl_appStatus.TextAlign =   
  123. System.Drawing.ContentAlignment.MiddleLeft;  
  124. //   
  125. // tbx_serviceName  
  126. //   
  127. this.tbx_serviceName.BorderStyle =   
  128. System.windows.Forms.BorderStyle.FixedSingle;  
  129. this.tbx_serviceName.ForeColor = System.Drawing.Color.Red;  
  130. this.tbx_serviceName.Location = new System.Drawing.Point(8, 8);  
  131. this.tbx_serviceName.Name = "tbx_serviceName";  
  132. this.tbx_serviceName.TabIndex = 5;  
  133. this.tbx_serviceName.Text = "輸入服務(wù)名";  
  134. this.tbx_serviceName.MouseDown += new 
  135.  System.windows.Forms.MouseEventHandler(  
  136. this.tbx_serviceName_MouseDown);  
  137. //   
  138. // btn_Exit  
  139. //   
  140. this.btn_Exit.Location = new System.Drawing.Point(256, 8);  
  141. this.btn_Exit.Name = "btn_Exit";  
  142. this.btn_Exit.Size = new System.Drawing.Size(64, 23);  
  143. this.btn_Exit.TabIndex = 6;  
  144. this.btn_Exit.Text = "退出程序";  
  145. this.btn_Exit.Click += new System.EventHandler(this.btn_Exit_Click);  
  146. //   
  147. // tbx_interval  
  148. //   C#啟動(dòng)Windows服務(wù)的窗體程序
  149. this.tbx_interval.BorderStyle =  
  150.  System.windows.Forms.BorderStyle.FixedSingle;  
  151. this.tbx_interval.ForeColor = System.Drawing.Color.Red;  
  152. this.tbx_interval.Location = new System.  
  153. Drawing.Point(8, 40);  
  154. this.tbx_interval.Name = "tbx_interval";  
  155. this.tbx_interval.TabIndex = 7;  
  156. this.tbx_interval.Text = "輸入監(jiān)控間隔(秒)";  
  157. this.tbx_interval.MouseDown += new 
  158.  System.windows.Forms.MouseEventHandler(  
  159. this.tbx_interval_MouseDown);  
  160. //   
  161. // timer1  
  162. //   
  163. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);  
  164. //   
  165. // notifyIcon1  
  166. //   
  167. this.notifyIcon1.Icon = ((System.Drawing.Icon)  
  168. (resources.GetObject("notifyIcon1.Icon")));  
  169. this.notifyIcon1.Text = "雙擊打開WatchService";  
  170. this.notifyIcon1.Visible = true;  
  171. this.notifyIcon1.DoubleClick +=   
  172. new System.EventHandler(this.notifyIcon1_DoubleClick);  
  173. //   
  174. // WatchService  
  175. //   C#啟動(dòng)Windows服務(wù)的窗體程序
  176. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);  
  177. this.ClientSize = new System.Drawing.Size(328, 102);  
  178. this.Controls.Add(this.tbx_interval);  
  179. this.Controls.Add(this.btn_Exit);  
  180. this.Controls.Add(this.tbx_serviceName);  
  181. this.Controls.Add(this.lbl_appStatus);  
  182. this.Controls.Add(this.btn_stopReg);  
  183. this.Controls.Add(this.btn_startReg);  
  184. this.Controls.Add(this.btn_stopWatch);  
  185. this.Controls.Add(this.btn_startWatch);  
  186. this.MaximizeBox = false;  
  187. this.Name = "WatchService";  
  188. this.ShowInTaskbar = false;  
  189. this.Text = "WatchService";  
  190. this.windowstate = System.windows.  
  191. Forms.Formwindowstate.Minimized;  
  192. this.Resize += new System.EventHandler(  
  193. this.WatchService_Resize);  
  194. this.Load += new System.EventHandler(  
  195. this.WatchService_Load);  
  196. this.ResumeLayout(false);  
  197. }  
  198. #endregion  
  199. private ServiceController scDBService;  
  200. private string serviceName="MSSqlServer";   
  201. // 默認(rèn)服務(wù)名  
  202. private int iInterval=60;  
  203.  // 默認(rèn)監(jiān)控間隔(秒)  
  204. /// ﹤summary﹥  
  205. ///C#啟動(dòng)Windows服務(wù)的窗體程序
  206. /// 應(yīng)用程序的主入口點(diǎn)。  
  207. /// ﹤/summary﹥  
  208. [STAThread]  
  209. static void Main()   
  210. {  
  211. Application.Run(new WatchService());  
  212. }  
  213. // 開啟監(jiān)控  
  214. private void btn_startWatch_Click(  
  215. object sender, System.EventArgs e)  
  216. {  
  217. if(this.tbx_serviceName.Text=="")  
  218. {  
  219. this.lbl_appStatus.Text="服務(wù)名不能為空";  
  220. this.tbx_serviceName.Focus();  
  221. return;  
  222. }  
  223. if(this.tbx_interval.Text=="")  
  224. {  
  225. this.lbl_appStatus.Text="服務(wù)監(jiān)控間隔不能為空";  
  226. this.tbx_interval.Focus();  
  227. return;  
  228. }  
  229. serviceName=this.tbx_serviceName.Text;  
  230. iInterval=int.Parse(this.tbx_interval.Text);  
  231. this.timer1.Interval=iInterval*1000;  
  232. startService();  
  233. }  
  234. // 開啟隨機(jī)啟動(dòng)  
  235. private void btn_startReg_Click(  
  236. object sender, System.EventArgs e)  
  237. {  
  238. try 
  239. {  
  240. string dir=Directory.GetCurrentDirectory();  
  241. dir+="\\WatchService.exe";  
  242. RegistryKey akey=Registry.LocalMachine;  
  243. akey=akey.OpenSubKey(@"SOFTWARE\Microsoft  
  244. \windows\CurrentVersion\Run",true);  
  245. akey.SetValue("WatchService",dir);  
  246. akey.Close();  
  247. this.lbl_appStatus.Text="開啟隨機(jī)啟動(dòng)成功。";  
  248. }  
  249. catch(Exception exp)  
  250. {  
  251. this.lbl_appStatus.Text="開啟隨機(jī)啟動(dòng)失敗,  
  252. 原因:"+exp.Message;  
  253. }  
  254. }  //C#啟動(dòng)Windows服務(wù)的窗體程序
  255. private void tbx_serviceName_MouseDown(  
  256. object sender, System.windows.Forms.MouseEventArgs e)  
  257. {  
  258. this.tbx_serviceName.Text="";  
  259. }  
  260. // 關(guān)閉隨機(jī)啟動(dòng)  
  261. private void btn_stopReg_Click(object sender,  
  262.  System.EventArgs e)  
  263. {  
  264. try 
  265. {  
  266. RegistryKey akey=Registry.LocalMachine;  
  267. akey=akey.OpenSubKey(@"SOFTWARE\Microsoft  
  268. \windows\CurrentVersion\Run",true);  
  269. akey.SetValue("WatchService",false);  
  270. akey.Close();  
  271. this.lbl_appStatus.Text="關(guān)閉隨機(jī)啟動(dòng)成功。";  
  272. }  
  273. catch(Exception exp)  
  274. {  
  275. this.lbl_appStatus.Text="關(guān)閉隨機(jī)啟動(dòng)失敗,原因:"+exp.Message;  
  276. }  
  277. }  
  278. private void btn_Exit_Click(object sender, System.EventArgs e)  
  279. {  
  280. Application.Exit();  
  281. }  
  282. /// ﹤summary﹥  
  283. /// 開啟指定的windows服務(wù)  
  284. ///C#啟動(dòng)Windows服務(wù)的窗體程序
  285. /// ﹤/summary﹥  
  286. private void startService()  
  287. {  
  288. try 
  289. {  
  290. scDBService=new ServiceController(serviceName);  
  291. ServiceController[] scAllService=  
  292. ServiceController.GetServices();  
  293. int i=0;  
  294. while(i﹤scAllService.Length)  
  295. {  
  296. if(scAllService[i].DisplayName==serviceName)  
  297. {  
  298. if(scDBService.Status.Equals(ServiceControllerStatus.Stopped))  
  299. {  
  300. this.lbl_appStatus.Text=serviceName+"  
  301. 服務(wù)正在啟動(dòng)……";  
  302. scDBService.Start();  
  303. }  
  304. else if(scDBService.Status.Equals(  
  305. ServiceControllerStatus.Running))  
  306. {  
  307. this.lbl_appStatus.Text=serviceName+"  
  308. 服務(wù)正在運(yùn)行……";  
  309. }  
  310. if(!this.timer1.Enabled) this.timer1.Start();  
  311. return;  
  312. }  
  313. else 
  314. {  
  315. i++;  
  316. }  
  317. }  
  318. this.lbl_appStatus.Text=serviceName+"  
  319. 服務(wù)并沒有安裝在本機(jī)上,請(qǐng)檢查。";  
  320. if(this.timer1.Enabled) this.timer1.Stop();  
  321. }  
  322. catch(Exception exp)  
  323. {  
  324. this.lbl_appStatus.Text=serviceName+  
  325. "服務(wù)啟動(dòng)失敗,原因:"+exp.Message;  
  326. }   
  327. }  
  328. // 監(jiān)控時(shí)鐘  
  329. private void timer1_Tick(object sender,   
  330. System.EventArgs e)  
  331. {  
  332. startService();   
  333. }  
  334. private void tbx_interval_MouseDown(object sender,  
  335.  System.windows.Forms.MouseEventArgs e)  
  336. {  
  337. this.tbx_interval.Text="";  
  338. }  
  339. // 窗體加載后即最小化到托盤區(qū)  
  340. private void WatchService_Load(  
  341. object sender, System.EventArgs e)  
  342. {  
  343. this.Hide();  
  344. this.notifyIcon1.Visible=true;  
  345. startService();  
  346. }  
  347. // 窗體最小化  
  348. private void WatchService_Resize(  
  349. object sender, System.EventArgs e)  
  350. {  
  351. if (this.windowstate==Formwindowstate.Minimized)  
  352. {  
  353. this.Hide();  
  354. this.notifyIcon1.Visible=true;  
  355. }  
  356. }  
  357. // 托盤區(qū)圖標(biāo)操作  
  358. private void notifyIcon1_DoubleClick(  
  359. object sender, System.EventArgs e)  
  360. {  
  361. this.Visible = true;  
  362. this.windowstate = Formwindowstate.Normal;  
  363. this.notifyIcon1.Visible = false;  
  364. }  
  365. // 停止監(jiān)控  
  366. private void btn_stopWatch_Click(  
  367. object sender, System.EventArgs e)  
  368. {  
  369. this.timer1.Stop();  
  370. }  
  371. }  

C#啟動(dòng)Windows服務(wù)的窗體程序的基本情況就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#啟動(dòng)Windows服務(wù)的窗體程序有所幫助。

【編輯推薦】

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

2009-08-25 09:39:21

創(chuàng)建C# Window

2009-09-02 17:28:26

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

2009-08-14 16:02:50

C#啟動(dòng)windows

2009-08-14 11:00:16

C#創(chuàng)建Windows

2009-09-02 17:53:42

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

2024-07-09 17:09:49

C#開發(fā)Windows窗體

2009-08-20 10:10:55

C#透明窗體

2009-09-07 06:07:46

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

2009-09-07 04:19:56

C#窗體事件

2009-09-07 04:56:52

C#模式窗體

2009-09-07 03:37:51

C#窗體

2009-09-07 05:31:39

C#窗體關(guān)閉事件

2009-09-07 06:56:46

C#透明窗體

2009-09-07 05:24:22

C#窗體繼承

2009-08-14 14:25:09

Windows服務(wù)程序

2009-08-14 15:06:08

Windows服務(wù)程序

2009-08-14 15:47:18

C#Windows服務(wù)

2009-08-14 15:19:38

Windows服務(wù)程序Windows服務(wù)

2009-08-14 14:17:16

C#Windows服務(wù)

2009-08-14 17:04:19

Windows后臺(tái)服務(wù)
點(diǎn)贊
收藏

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