English 中文(简体)
时间的微粒或体格达到一定限度――如何? (C#)
原标题:Time elapses or integer reaches a specified limit - how? (C#)

I m 运行一个C#方案(假释申请,很快将转换为Windows Service),在该方案中,我需要能够就服务中的错误向管理人员发出电子邮件,但是,如果过去几分钟的错误数超过4-5,我就不必寄给我们一个电子邮件,指出存在多种错误。

我的理解是,我是在某种方式中使用一个时间,但任何人都能够提供更为具体的建议? 我非常感谢

最佳回答

由MSDN修改。 http://www.un.org/Depts/DGACM/index_french.htm

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);
        }
    }
}
问题回答

如果你跟踪你使用数据库发送的每封电子邮件,你就能够总是对数据库进行勘测,以了解你在一定时期内看到的某一错误。 在少数几个项目中,我曾努力在什么地方进行电子邮件,发送电子邮件的登记一直是姐妹要求,从而解决你的问题。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签