首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >时间流逝或整数达到指定的限制-如何?(C#)

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

Stack Overflow用户
提问于 2010-10-01 21:06:32
回答 3查看 431关注 0票数 0

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

我知道我会以某种形式使用计时器,但有人能提供更具体的建议吗?我将非常感激

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-10-01 21:19:51

从MSDN修改。注意关于Timer object aTimer的声明和清理的注释。

代码语言:javascript
运行
复制
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);
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2010-10-01 21:13:04

如果你使用数据库跟踪你发送的每一封电子邮件,你总是可以轮询数据库,看看在给定的一段时间内,你看到了多少电子邮件,等等。在我工作过的几个项目中,电子邮件是一个要求,记录发送的电子邮件一直是一个姐妹要求,因此创建了一个问题的解决方案。

票数 0
EN

Stack Overflow用户

发布于 2010-10-01 21:14:12

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

传递包装SendEmail方法的委托。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3839416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档