时间流逝或整数达到指定限制-如何?(C#)
我正在运行一个C#程序(控制台应用程序,很快将转换为Windows服务),在该程序中,我需要能够向管理员发送有关服务中错误的电子邮件,但如果过去几分钟内的错误数超过4-5,我需要它不要为每个错误发送电子邮件,因此,它只会发送一封电子邮件,说明存在多个错误时间流逝或整数达到指定限制-如何?(C#),c#,timer,if-statement,C#,Timer,If Statement,我正在运行一个C#程序(控制台应用程序,很快将转换为Windows服务),在该程序中,我需要能够向管理员发送有关服务中错误的电子邮件,但如果过去几分钟内的错误数超过4-5,我需要它不要为每个错误发送电子邮件,因此,它只会发送一封电子邮件,说明存在多个错误 我知道我会用某种形式的计时器,但有人能提供更具体的建议吗?我非常感激如果您使用数据库跟踪发送的每封电子邮件,您可以随时轮询数据库,查看您在给定时间段内看到的给定错误的电子邮件数量等。在我工作过的几个项目中,电子邮件是一项要求,发送电子邮件的日志
我知道我会用某种形式的计时器,但有人能提供更具体的建议吗?我非常感激如果您使用数据库跟踪发送的每封电子邮件,您可以随时轮询数据库,查看您在给定时间段内看到的给定错误的电子邮件数量等。在我工作过的几个项目中,电子邮件是一项要求,发送电子邮件的日志记录一直是一项姐妹要求,从而创建问题的解决方案。使用将错误保存在列表中,然后使用
传递包装SendEmail方法的委托。从MSDN修改。请注意有关
计时器对象aTimer
的声明和清理的注释
using System;
using System.Timers;
using System.Threading;
public class Timer2
{
private static System.Timers.Timer aTimer;
private static List<string> errors = new List<string>();
private static readonly int interval = 300000; // 5 minutes at present
private static readonly int trigger = 10; // send msg if > 10 errors
// Message processing - error detection
public static void processMessage(Message message)
{
// do the work here
// then check error
if (message.HasError)
{
// add error to pending list
lock (errors)
{
string newErrorData = "got another one!";
errors.Add(newErrorData);
++trigger;
}
}
}
public static void Main()
{
// Normally, the timer is declared at the class level,
// so that it stays in scope as long as it is needed.
// If the timer is declared in a long-running method,
// KeepAlive must be used to prevent the JIT compiler
// from allowing aggressive garbage collection to occur
// before the method ends. (See end of method.)
//System.Timers.Timer aTimer;
// Create a timer with specified interval.
aTimer = new System.Timers.Timer(interval);
// Hook up the event handler for the Elapsed event.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
// Kick off message handling - don't forget to clean up the timer when
// you wish to exit
while (moreMessages)
{
Message message = getNextmessage();
ProcessMessage(message);
}
// cleanup here when messages are drained
// If the timer is declared in a long-running method, use
// KeepAlive to prevent garbage collection from occurring
// before the method ends.
//GC.KeepAlive(aTimer); }
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
object errorEmail = null;
lock (errors)
{
if (errors.Count > trigger)
{
// init message to contain errors here
errorEmail = new ErrorEmail();
foreach (string err in errors)
{
// add error info to message
}
errors.Clear();
trigger = 0;
}
}
if (errorEmail != null)
{
// send message outside the lock
Send(errorEmail);
}
}
}
使用系统;
使用系统计时器;
使用系统线程;
公共类计时器2
{
专用静态系统定时器定时器定时器;
私有静态列表错误=新列表();
私有静态只读int interval=300000;//目前为5分钟
private static readonly int trigger=10;//错误>10时发送消息
//消息处理-错误检测
公共静态void processMessage(消息消息消息)
{
//在这里工作
//然后检查错误
if(message.HasError)
{
//将错误添加到挂起列表
锁定(错误)
{
string newErrorData=“获得另一个!”;
错误。添加(newErrorData);
++触发;
}
}
}
公共静态void Main()
{
//通常,计时器是在类级别声明的,
//所以只要需要它,它就在范围内。
//如果计时器是在长时间运行的方法中声明的,
//必须使用KeepAlive来阻止JIT编译器
//从允许积极的垃圾收集发生
//在方法结束之前。(请参见方法结束。)
//系统定时器定时器定时器;
//创建具有指定间隔的计时器。
aTimer=新系统计时器计时器(间隔);
//为经过的事件连接事件处理程序。
aTimer.Appead+=新的ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled=true;
//启动消息处理-在启动时不要忘记清理计时器
//你想退出吗
while(更多信息)
{
Message Message=getNextmessage();
ProcessMessage(消息);
}
//当消息被清空时,在此进行清理
//如果计时器是在长时间运行的方法中声明的,请使用
//KeepAlive可防止发生垃圾回收
//在方法结束之前。
//GC.KeepAlive(aTimer);}
私有静态void OnTimedEvent(对象源,ElapsedEventArgs e)
{
对象errorEmail=null;
锁定(错误)
{
如果(errors.Count>触发器)
{
//初始化消息以在此处包含错误
errorEmail=新的errorEmail();
foreach(错误中的字符串错误)
{
//将错误信息添加到消息中
}
错误。清除();
触发器=0;
}
}
if(errorEmail!=null)
{
//在锁外发送消息
发送(电子邮件);
}
}
}
您可以使用嵌入式数据库,如SQLite()