时间流逝或整数达到指定限制 - 如何? (C#)

时间:2010-10-01 13:06:32

标签: c# timer if-statement

我正在运行一个C#程序(控制台应用程序,很快将被转换为Windows服务),我需要能够向管理员发送有关服务中的错误的电子邮件,但我需要它不向每个人发送电子邮件如果最近几分钟内的错误数超过4-5,则会出错,因此它只会发送一封电子邮件,说明存在多个错误。

我知道我会在某种形式下使用计时器,但任何人都可以提供更具体的建议吗?我会非常感激

3 个答案:

答案 0 :(得分:0)

如果您使用数据库跟踪发送的每封电子邮件,您可以随时轮询数据库以查看在给定时间段内针对给定错误看到的电子邮件数量等。在我参与过的少数项目中电子邮件是一项要求,记录已发送的电子邮件一直是姐妹的要求,从而为您的问题创建解决方案。

答案 1 :(得分:0)

使用保存列表中的错误,然后使用System.Threading.Timer

传递一个包装SendEmail方法的委托。

答案 2 :(得分:0)

从MSDN修改。请注意有关Timer对象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);
        }
    }
}