C#console应用程序计时器问题

时间:2012-05-29 14:02:42

标签: c# .net timer

我有一个由Windows服务调用的控制台应用程序。此控制台应用程序基于某些App.config文件条目创建System.Timers.Timer的实例(我已创建自定义App.config部分,并且计时器实例的数量将与此部分中的元素的数量相同)。预计控制台应用程序不会关闭 - 如果由于某种原因关闭,Windows服务将再次调用它。

为了使控制台应用程序永远存在,我将无限循环写为控制台应用程序的最后一个语句。而(1 == 1){}。

问题是,我看到控制台应用程序每5分钟终止一次。我不明白为什么会这样。

如果有更好的方法,请建议。

代码

static void Main(string [] args)         {

        Boolean _isNotRunning;
        using (Mutex _mutex = new Mutex(true, _mutexID, out _isNotRunning))
        {
            if (_isNotRunning)
            {
                new ProcessScheduler().InitializeTimers();                             
                while (1 == 1) { }                                                         
            }
            else
            {  
                return;
            }
        }

公共类ProcessScheduler     {

public void InitializeTimers()
    {
        XYZConfigSection.XYZAppSection section =  (XYZConfigSection.XYZAppSection)ConfigurationManager.GetSection("XYZApp");        
        if (section != null)
        {
            XYZComponentTimer XYZComponentTimer = null;
            for (int intCount = 0; intCount < section.XYZComponents.Count; intCount++)
            {
                XYZComponentTimer = new XYZComponentTimer();
                XYZComponentTimer.ComponentId = section.XYZComponents[intCount].ComponentId;
                XYZComponentTimer.Interval = int.Parse(section.XYZComponents[intCount].Interval);
                XYZComponentTimer.Elapsed += new ElapsedEventHandler(XYZComponentTimer_Elapsed);
                XYZComponentTimer.Enabled = true;                    
            }
        }
    }

}

public class XYZComponentTimer:Timer
{        
    public string ComponentId { get; set; }
}        

更新

如代码中所述,每个实例的计时器间隔是根据相应元素的配置文件值设置的。现在,配置文件中有两个部分:一个间隔为15秒,另一个间隔为10秒。

3 个答案:

答案 0 :(得分:3)

让我猜一下:定时器间隔为5分钟,定时器崩溃导致进程退出。

为什么不记录任何崩溃或附加调试器?

答案 1 :(得分:1)

处理AppDomain.UnhandledException event并记录异常。

答案 2 :(得分:0)

由于一些奇怪的原因,Mutex每5分钟被实例化一次,并且正在将_isNotRunning设置为true。那就是问题。