namespace MonitorService
{
public partial class MonitorSv : ServiceBase
{
string AppName = "",MusicName = "";
string apppath = "";
Thread threadwork;
SoundPlayer player;
public MonitorSv()
{
InitializeComponent();
string path = Directory.GetCurrentDirectory()+"\\Resources\\XML\\AppFile.xml";// Directory.GetCurrentDirectory() + "\\Resources\\XML\\AppFile.xml";
string[] names = RwXmlBs.ReadXml(path);
if (names != null && names.Length == )
this.AppName = names[];
this.MusicName = names[];
//Beep(1000, 10000);//调试可以响,但是安装后不响,所以改成播放声音 //WriteFile(names[0] + "<=>" + names[1] + "++path:" + path, 0, "构造函数");
PlayMusic(); } private void PlayMusic()
{
if (String.IsNullOrEmpty(MusicName))
return;
player = new SoundPlayer();
player.SoundLocation = @"D:\\spring.wav";// MusicName;// @""+ MusicName + "";
player.Load(); //同步加载声音
player.Play(); //启用新线程播放
} private void PlayerStop()
{
if (player != null)
{
player.Stop();
}
}
protected override void OnStart(string[] args)
{
if (threadwork == null)
{
threadwork = new Thread(CheckAppMethod);
threadwork.IsBackground = true;
threadwork.Start();
}
} private void CheckAppMethod()
{
Process[] ps = null;
try
{
while (true)
{
if (!String.IsNullOrEmpty(AppName))
{
ps = Process.GetProcessesByName(AppName); // "MakeCard"); //不用带.exe
if (ps.Length <= )//进程被杀死,则报警.
{
PlayMusic();
//Been(500,1000);//不可以响,搞不懂
}
else
{
PlayerStop();
}
}
System.Threading.Thread.Sleep();
}
}
catch (Exception EX)
{
WriteFile(AppName, ps.Length, "异常的"+EX.ToString());
OnShutdown();
}
} private void WriteFile(string AppName,int Length, string EX)
{
try
{
FileStream fs = new FileStream(apppath+"\\CloseFileLog.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(, SeekOrigin.End);
sw.WriteLine("AppName:" + AppName + "++ps.Length:" + Length + ">>>" + DateTime.Now.ToString() + EX.ToString() + "\n");
sw.Flush();
sw.Close();
fs.Close();
}
catch (Exception ex)
{ }
} protected override void OnShutdown()
{
base.OnShutdown();
} protected override void OnStop()
{
GC.Collect();
PlayerStop();
} // 第一个参数是指频率的高低,越大越高,第二个参数是指响的时间多长
[DllImport("kernel32.dll", EntryPoint = "Beep")]
public static extern int Beep(int dwFreq,int dwDuration);
}
}

c# windows service 实现监控其他程序是否被关闭,关闭则报警的更多相关文章

  1. .NET开发Windows Service程序 - Topshelf

    在实际项目开发过程中,会经常写一些类似定时检查,应用监控的应用.这类应用在windows平台通常都会写成window service程序. 在百度上搜索一下'c#开发windows service', ...

  2. C# 创建Windows Service(Windows服务)程序

    本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...

  3. 如何利用mono把.net windows service程序迁移到linux上

    How to migrate a .NET Windows Service application to Linux using mono? 写在最前:之所以用要把windows程序迁移到Linux上 ...

  4. 让自己的C++程序(非服务程序)运行为一个windows service

    因为项目的一些变化和原因,需要把数据处理的一个后台程序创建为一个windows服务,运行以下命令能创建成功: sc create "MyApp Service Name" binP ...

  5. C#中级-Windows Service程序安装注意事项

    一.前言 这周除了改写一些识别算法外,继续我的Socket服务编写.服务器端的Socket服务是以Windows Service的形式运行的. 在我完成Windows Service编写后,启动服务时 ...

  6. C#中级-通过注册表读取Windows Service程序执行路径

    一.前言        假设我们的C#解决方案中有多个程序应用,如:Web应用.控制台程序.WPF程序应用和Windows服务应用. 那么这些非Windows Service应用程序怎么在代码中找到W ...

  7. C#Windows Service程序的创建安装与卸载

    C#Windows Service程序的创建安装与卸载 一.开发环境 操作系统:Windows7x64 sp1 专业版 开发环境:Visual studio 2013 编程语言:C# .NET版本: ...

  8. windows service程序的Environment.CurrentDirectory路径

    当前工作目录Environment.CurrentDirectory,对于winform程序,其是在程序放置的目录里, 而windows service的Environment.CurrentDire ...

  9. Windows下tomcat进程监控批处理程序

    在Windows下tomcat进程监控批处理程序脚本如下: @echo off ::tomcat安装目录 set _tomcatDir=E:\myFiles\apache-tomcat-8.5.31 ...

随机推荐

  1. 动态增加表单元素并获取元素的text和value提交

    以上是效果图 需求是这样的: 专家设置好条件,然后设备检测到达到相应的条件之后,设备发出提醒给用户. 这就需要专家设置好能看懂的条件之后,然后把给专家看的,正常人能看懂的条件和发送的设备的,设备能够识 ...

  2. 百度在线编辑器 - PHP获取提交的数据

    原文:http://www.upwqy.com/details/14.html 1 我们知道在在百度在线编辑器的demo中. 我们只要在body 里面 加载 script 标签 id="ed ...

  3. PHP 秒数 转时分秒 函数

    function secondsToHour($seconds){ if(intval($seconds) < 60) $tt ="00时00分".sprintf(" ...

  4. Rotational Region CNN

    R2CNN 论文Rotational Region CNN for Orientation Robust Scene Text Detection与RRPN(Arbitrary-Oriented Sc ...

  5. 线段树 (区间查询最大 区间求和 区间加)带lazy

    ; struct Segment_tree { struct Node { int val,Max,lazy; ]; void init() { lazy=son[]=son[]=Size=val=M ...

  6. AJAX跨域问题解决思路

    ajax跨域问题的解决思路主要分为3种: 1.浏览器限制解决思路:不让浏览器做出限制解决方法:通过指定参数,让浏览器不做跨域校验评价:价值不大,需要每个人都做改动,而且改动是客户端的改动 2.XHR请 ...

  7. 利用sfc文件构建网络渗透

      收集哈希 SCF(Shell命令文件)文件可用于执行一组有限的操作,例如显示Windows桌面或打开Windows资源管理器,这并不是什么新鲜事.然而,一个SCF文件可以用来访问一个特定的UNC路 ...

  8. java创建运行以及项目结构

    一 创建java project 再src下添加class,选择一个class添加main方法作为程序的入口 二.项目结构: src下添加不同的包,命名方法为com.jikexueyuan.hello ...

  9. 【译】Java、Kotlin、RN、Flutter 开发出来的 App 大小,你了解过吗?

    现在开发 App 的方式非常多,原生.ReactNative.Flutter 都是不错的选择.那你有没有关注过,使用不同的方式,编译生成的 Apk ,大小是否会有什么影响呢?本文就以一个最简单的 He ...

  10. Nginx目录浏览功能

    要给其他人提供一个patch的下载地址,于是想用nginx的目录浏览功能来做,需要让其他人看到指定一个目录下的文件列表,然后让他自己来选择该下载那个文件:效果如图. 实现步骤:在虚拟主机配置文件里面开 ...