posts - 2,  comments - 0,  trackbacks - 0

最近,做了一个定时监控WINDOWS服务的程序,要做到随机启动,定时监控指定的服务,如果没有启动则开启,现在正在测试中,现在将代码及思路公布

一、思路

        本程序的核心在于随机启动和WINDOWS服务上,对于随机启动,引入Microsoft.Win32命名空间,利用RegistryKey类即可完成对注册表的增、删、改等操作;对于WINDOWS服务,引入System.ServiceProcess命名空间,利用ServiceController类即可完成对系统服务的启动、停止、查询等操作。
二、
        代码如下,这里程序默认为开启MSSQLSERVER服务,并添加了托盘区图标,可以在启动时或启动后最小化到托盘区:

  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.windows.Forms;
  6using System.Data;
  7using System.ServiceProcess;
  8using System.IO;
  9using Microsoft.Win32;
 10namespace WatchService
 11{
 12/// <summary>
 13/// Form1 的摘要说明。
 14/// </summary>

 15public class WatchService : System.windows.Forms.Form
 16{
 17private System.windows.Forms.Button btn_startWatch;
 18private System.windows.Forms.Button btn_stopWatch;
 19private System.windows.Forms.Button btn_startReg;
 20private System.windows.Forms.Button btn_stopReg;
 21private System.windows.Forms.Label lbl_appStatus;
 22private System.windows.Forms.TextBox tbx_serviceName;
 23private System.windows.Forms.Button btn_Exit;
 24private System.windows.Forms.TextBox tbx_interval;
 25private System.windows.Forms.Timer timer1;
 26private System.windows.Forms.NotifyIcon notifyIcon1;
 27private System.ComponentModel.IContainer components;
 28public WatchService()
 29{
 30//
 31// windows 窗体设计器支持所必需的
 32//
 33InitializeComponent();
 34//
 35// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 36//
 37}

 38/// <summary>
 39/// 清理所有正在使用的资源。
 40/// </summary>

 41protected override void Dispose( bool disposing )
 42{
 43if( disposing )
 44{
 45    if (components != null
 46    {
 47    components.Dispose();
 48    }

 49}

 50base.Dispose( disposing );
 51}

 52windows 窗体设计器生成的代码
179private ServiceController scDBService;
180private string serviceName="MSSqlServer"// 默认服务名
181private int iInterval=60// 默认监控间隔(秒)
182/// <summary>
183/// 应用程序的主入口点。
184/// </summary>

185[STAThread]
186static void Main() 
187{
188Application.Run(new WatchService());
189}

190// 开启监控
191private void btn_startWatch_Click(object sender, System.EventArgs e)
192{
193if(this.tbx_serviceName.Text=="")
194{
195    this.lbl_appStatus.Text="服务名不能为空";
196    this.tbx_serviceName.Focus();
197    return;
198}

199if(this.tbx_interval.Text=="")
200{
201    this.lbl_appStatus.Text="服务监控间隔不能为空";
202    this.tbx_interval.Focus();
203    return;
204}

205serviceName=this.tbx_serviceName.Text;
206iInterval=int.Parse(this.tbx_interval.Text);
207this.timer1.Interval=iInterval*1000;
208startService();
209}

210// 开启随机启动
211private void btn_startReg_Click(object sender, System.EventArgs e)
212{
213try
214{
215    string dir=Directory.GetCurrentDirectory();
216    dir+="\\WatchService.exe";
217    RegistryKey akey=Registry.LocalMachine; 
218    akey=akey.OpenSubKey(@"SOFTWARE\Microsoft\windows\CurrentVersion\Run",true); 
219    akey.SetValue("WatchService",dir);
220    akey.Close();
221    this.lbl_appStatus.Text="开启随机启动成功。";
222}

223catch(Exception exp)
224{
225    this.lbl_appStatus.Text="开启随机启动失败,原因:"+exp.Message;
226}

227}

228private void tbx_serviceName_MouseDown(object sender, System.windows.Forms.MouseEventArgs e)
229{
230this.tbx_serviceName.Text="";
231}

232// 关闭随机启动
233private void btn_stopReg_Click(object sender, System.EventArgs e)
234{
235try
236{
237    RegistryKey akey=Registry.LocalMachine; 
238    akey=akey.OpenSubKey(@"SOFTWARE\Microsoft\windows\CurrentVersion\Run",true); 
239    akey.SetValue("WatchService",false);
240    akey.Close();
241    this.lbl_appStatus.Text="关闭随机启动成功。";
242}

243catch(Exception exp)
244{
245    this.lbl_appStatus.Text="关闭随机启动失败,原因:"+exp.Message;
246}
 
247}

248private void btn_Exit_Click(object sender, System.EventArgs e)
249{
250Application.Exit();
251}

252/// <summary>
253/// 开启指定的windows服务
254/// </summary>

255private void startService()
256{
257try
258{
259    scDBService=new ServiceController(serviceName);
260    ServiceController[] scAllService=ServiceController.GetServices();
261    int i=0;
262    while(i<scAllService.Length)
263    {
264    if(scAllService[i].DisplayName==serviceName)
265    {
266      if(scDBService.Status.Equals(ServiceControllerStatus.Stopped))
267      {
268      this.lbl_appStatus.Text=serviceName+"服务正在启动……";
269      scDBService.Start();
270      }

271      else if(scDBService.Status.Equals(ServiceControllerStatus.Running))
272      {
273      this.lbl_appStatus.Text=serviceName+"服务正在运行……";
274      }

275      if(!this.timer1.Enabled) this.timer1.Start();
276      return;
277    }

278    else
279    {
280      i++;
281    }

282    }

283    this.lbl_appStatus.Text=serviceName+"服务并没有安装在本机上,请检查。";
284    if(this.timer1.Enabled) this.timer1.Stop();
285}

286catch(Exception exp)
287{
288    this.lbl_appStatus.Text=serviceName+"服务启动失败,原因:"+exp.Message;
289}
 
290}

291// 监控时钟
292private void timer1_Tick(object sender, System.EventArgs e)
293{
294startService(); 
295}

296private void tbx_interval_MouseDown(object sender, System.windows.Forms.MouseEventArgs e)
297{
298this.tbx_interval.Text="";
299}

300// 窗体加载后即最小化到托盘区
301private void WatchService_Load(object sender, System.EventArgs e)
302{
303this.Hide();
304this.notifyIcon1.Visible=true;
305startService();
306}

307// 窗体最小化
308private void WatchService_Resize(object sender, System.EventArgs e)
309{
310if (this.windowstate==Formwindowstate.Minimized)
311{
312    this.Hide();
313    this.notifyIcon1.Visible=true;
314}
    
315}

316// 托盘区图标操作
317private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
318{
319this.Visible = true;
320this.windowstate = Formwindowstate.Normal;
321this.notifyIcon1.Visible = false
322}

323// 停止监控
324private void btn_stopWatch_Click(object sender, System.EventArgs e)
325{
326this.timer1.Stop();
327}

328}

329}
posted on 2009-06-11 13:29 Deo.Feng 阅读(390) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2025年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(1)

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜