c# windows service 实现监控其他程序是否被关闭,关闭则报警

  1 namespace MonitorService
  2 {
  3     public partial class MonitorSv : ServiceBase
  4     {
  5         string AppName = "",MusicName = "";
  6         string apppath = "";
  7         Thread threadwork;
  8         SoundPlayer player;
  9         public MonitorSv()
 10         {
 11             InitializeComponent();
 12             string path = Directory.GetCurrentDirectory()+"\\Resources\\XML\\AppFile.xml";// Directory.GetCurrentDirectory() + "\\Resources\\XML\\AppFile.xml";
 13             string[] names = RwXmlBs.ReadXml(path);
 14             if (names != null && names.Length == 2)
 15             this.AppName = names[0];
 16             this.MusicName = names[1];
 17             //Beep(1000, 10000);//调试可以响,但是安装后不响,所以改成播放声音
 18 
 19             //WriteFile(names[0] + "<=>" + names[1] + "++path:" + path, 0, "构造函数");
 20             PlayMusic();
 21 
 22         }
 23 
 24         private void PlayMusic()
 25         {
 26             if (String.IsNullOrEmpty(MusicName))
 27                 return;
 28             player = new SoundPlayer();
 29             player.SoundLocation = @"D:\\spring.wav";// MusicName;// @""+ MusicName + "";
 30             player.Load(); //同步加载声音  
 31             player.Play(); //启用新线程播放           
 32         }
 33 
 34         private void PlayerStop()
 35         {
 36             if (player != null)
 37             {
 38                 player.Stop();
 39             }
 40         }
 41         protected override void OnStart(string[] args)
 42         {
 43             if (threadwork == null)
 44             {
 45                 threadwork = new Thread(CheckAppMethod);
 46                 threadwork.IsBackground = true;
 47                 threadwork.Start();
 48             }
 49         }
 50 
 51         private void CheckAppMethod()
 52         {
 53             Process[] ps = null;
 54             try
 55             {
 56                 while (true)
 57                 {
 58                     if (!String.IsNullOrEmpty(AppName))
 59                     {
 60                         ps = Process.GetProcessesByName(AppName); // "MakeCard"); //不用带.exe
 61                         if (ps.Length <= 0)//进程被杀死,则报警.
 62                         {
 63                             PlayMusic();
 64                             //Been(500,1000);//不可以响,搞不懂
 65                         }
 66                         else
 67                         {
 68                             PlayerStop();
 69                         }
 70                     }
 71                     System.Threading.Thread.Sleep(5000);
 72                 }
 73             }
 74             catch (Exception EX)
 75             {
 76                 WriteFile(AppName, ps.Length, "异常的"+EX.ToString());
 77                 OnShutdown();
 78             }
 79         }
 80 
 81         private void WriteFile(string AppName,int Length, string EX)
 82         {
 83             try
 84             {
 85                 FileStream fs = new FileStream(apppath+"\\CloseFileLog.txt", FileMode.OpenOrCreate, FileAccess.Write);
 86                 StreamWriter sw = new StreamWriter(fs);
 87                 sw.BaseStream.Seek(0, SeekOrigin.End);
 88                 sw.WriteLine("AppName:" + AppName + "++ps.Length:" + Length + ">>>" + DateTime.Now.ToString() + EX.ToString() + "\n");
 89                 sw.Flush();
 90                 sw.Close();
 91                 fs.Close();
 92             }
 93             catch (Exception ex)
 94             {
 95 
 96             }
 97         }
 98 
 99         protected override void OnShutdown()
100         {
101             base.OnShutdown();
102         }
103 
104         protected override void OnStop()
105         {
106             GC.Collect();
107             PlayerStop();
108         }
109 
110         // 第一个参数是指频率的高低,越大越高,第二个参数是指响的时间多长  
111      [DllImport("kernel32.dll", EntryPoint = "Beep")]      
112      public static extern int Beep(int dwFreq,int dwDuration);
113     }
114 }
View Code

 

posted @ 2017-07-06 14:07  longdb  阅读(2641)  评论(0编辑  收藏  举报