繁体   English   中英

C#控制台应用程序计时器问题

[英]C# console application timer issue

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

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

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

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

静态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秒。

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

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

处理AppDomain.UnhandledException事件并记录异常。

由于某种奇怪的原因,互斥体每5分钟实例化一次,并将_isNotRunning设置为true。 那就是问题所在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM