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. 百度统计&友盟统计

    一.百度统计 登录百度站长统计账号-->管理 --->代码获取-->复制代码,如 <script> var _hmt = _hmt || []; (function() ...

  2. MongoDB存储引擎(中)——WiredTiger

    上一篇博文介绍了MongoDB的MMAPv1存储引擎,本文接着介绍MongoDB另一个存储引擎--WiredTiger,WiredTiger是在MongoDB3.0版本引入的,并且在MongoDB3. ...

  3. JDK源码及其他框架源码解析随笔地址导航

    置顶一篇文章,主要是整理一下写过的JDK中各个类的源码及其他框架源码解析的文章,方便自己随时阅读也方便网友朋友们阅读与指正 基础篇 从为什么String=String谈到StringBuilder和S ...

  4. WPF研究之道——数据驱动UI

    如果有人问你wpf和winform的区别,也许你会说,wpf的界面比较漂亮,wpf有诸多新的理念,的确如此.我今天想说的是wpf的数据驱动UI的理念. 传统的winform,想要更新界面内容,是不是必 ...

  5. Lintcode360 Sliding Window Median solution 题解

    [题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...

  6. Vuejs实例-使用vue-cli创建项目

    1,首先从官方网站下载安装Node.js,建议使用6.x版本,同时也会一并安装npm工具,npm>3.10以上. 2,npm安装很慢(国外服务器),所以一般推荐使用npm淘宝镜像cnpm,先安装 ...

  7. WordPress添加个性化的博客宠物的方法

    在很多的网站上都看见过这种效果,于是自己也想试试.看见我网站上的小宠物了吗,就是这种效果. 不多说,方法如下: 工具: 下载地址:http://yunpan.cn/cFUmZB8WWthty 访问密码 ...

  8. Excel IF函数怎么用

    本例主要介绍Excel表格中IF函数的用法,包括基本用法.单条件.多条件表达及在数组函数中的用法和在数组函数中怎么表达多条件和单条件. 工具/原料   Excel IF函数语法介绍:   1 IF函数 ...

  9. python全栈开发-Day3 字符串

    python全栈开发-Day3 字符串 一.按照以下几个点展开字符串的学习  #一:基本使用 1. 用途 #首先字符串主要作用途径:名字,性别,国籍,地址等描述信息2.定义方式 在单引号\双引号\三引 ...

  10. php基础知识(三)---常用函数--2017-04-16

    常用函数如下:(红色为重点) 1.取字符串的长度 echo strlen("hello"); 2.echo strcmp("字符串1","字符串2&q ...