如果Windows服务停止,给我发电子邮

如果Windows服务停止,给我发电子邮,第1张

概述如果Windows服务停止,给我发电子

我希望能够检查某个服务是否正在运行(假设它有一个显示名称 – ServiceA)。 我希望我的程序每隔5分钟检查一次服务是否仍在运行。 如果没事的话,会循环再等5分钟再检查一次。 如果发现ServiceA已经停止,我希望程序给我发电子邮件,并说… ServiceA已经停止运行。 下面我有我已经完成的代码,它能够将所有当前正在运行的服务和实际的显示名称返回到控制台。 任何人在上面我需要的代码/逻辑的任何想法?

namespace ServicesChecker { class Program { static voID Main(string[] args) { ServiceController[] scServices; scServices = ServiceController.GetServices(); Console.Writeline("Services running on the local computer:"); foreach (ServiceController scTemp in scServices) { if (scTemp.Status == ServiceControllerStatus.Running) { Console.Writeline(); Console.Writeline(" Service : {0}",scTemp.Servicename); Console.Writeline(" display name: {0}",scTemp.displayname); } } //Create a Pause.... Console.Readline(); } } }

windows微秒计算精度

实施可靠的日志

2应用程序最重要的问题

如何使用fsharpi(fsi)和mono的stdinpipe道

连接到没有用户名/密码的远程机器

把每个服务的名字放在一个数组中,并检查你想要的名字是否正在运行

List<string> arr = new List<string>(); foreach (ServiceController scTemp in scServices) { if (scTemp.Status == ServiceControllerStatus.Running) { arr.add(scTemp.Servicename); } } if (arr.Contains("YourWantedname") { // loop again } else { // send mail }

如果你知道你正在寻找哪一个服务,就不需要迭代所有的服务:你可以使用服务名来实例化ServiceController 。

至于发送电子邮件:看看System.Net.Mail.MailMessage类。

NB:你知道你也可以配置服务来触发一个动作,如果失败了?

您需要跟踪需要某种存储的服务状态。 最简单的可能是跟踪服务状态的XML文件,也许是这样的模式

<services> <service name="service1" last-check="12/21/2011 13:00:05" last-status="running" /> ... </services>

您的监控应用程序将会醒来,找到其感兴趣的服务的状态,并检查该服务的以前的状态。 如果状态正在运行,但当前已停止,请发送电子邮件。 如果找不到服务,请将其添加到服务列表中。

当您的监控应用程序停机时,将服务状态保存到磁盘可以保护您。

下面是一个很类似的服务的例子。 应该很简单,以适应您的需求..

public partial class CrowdCodeService : ServiceBase { private Timer stateTimer; private TimerCallback timerDelegate; autoresetEvent autoEvent = new autoresetEvent(false); public CrowdCodeService() { InitializeComponent(); } int secondsDefault = 30; int secondsIncrementError = 30; int secondesMaximum = 600; int seconds; protected overrIDe voID OnStart(string[] args) { Loggy.Add("Starting CrowdCodeService."); timerDelegate = new TimerCallback(DoSomething); seconds = secondsDefault; stateTimer = new Timer(timerDelegate,autoEvent,seconds * 1000); } static bool isRunning = false; // The state object is necessary for a TimerCallback. public voID DoSomething(object stateObject) { if (CrowdCodeService.isRunning) { return; } CrowdCodeService.isRunning = true; autoresetEvent autoEvent = (autoresetEvent)stateObject; try { ////// Do your work here string cs = "Application"; EventLog elog = new EventLog(); if (!EventLog.sourceExists(cs)) { EventLog.CreateEventSource(cs,cs); } elog.source = cs; elog.EnableRaisingEvents = true; elog.WriteEntry("CrowdCodes Service Error:" + cmd.Message.ToString(),EventLogEntryType.Error,991); } } finally { CrowdCodeService.isRunning = false; } } protected overrIDe voID OnStop() { Loggy.Add("Stopped CrowdCodeService."); stateTimer.dispose(); } }

总结

以上是内存溢出为你收集整理的如果Windows服务停止,给我发电子邮全部内容,希望文章能够帮你解决如果Windows服务停止,给我发电子邮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1291871.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存