简体   繁体   English

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

[英]C# console application timer issue

I have a console applciation which is invoked by a Windows Service. 我有一个由Windows服务调用的控制台应用程序。 This console application creates instances of System.Timers.Timer based on certain App.config file entries (I have created a custom App.config section and the number of timer instances will be same as that of the elements in this section). 该控制台应用程序基于某些App.config文件条目创建System.Timers.Timer的实例(我已经创建了一个自定义App.config部分,计时器实例的数量将与本部分中的元素相同)。 The console application is expected not to close - if it closes for some reason, the windows service will invoke it again. 预期控制台应用程序不会关闭-如果由于某种原因关闭它,则Windows服务将再次调用它。

To make the console application live for ever, I have an infinite loop written as the last statement of the console application. 为了使控制台应用程序永远存在,我编写了一个无限循环作为控制台应用程序的最后一条语句。 while (1 == 1) { }. 而(1 == 1){}。

The issue is, I see that the console application terminates every 5 minutes. 问题是,我看到控制台应用程序每5分钟终止一次。 I don't understand why is this happening. 我不明白为什么会这样。

If there are any better approaches, please suggest. 如果有更好的方法,请提出建议。

Code

static void Main(string[] args) { 静态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;
            }
        }

public class ProcessScheduler { 公共类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; }
}        

Update: 更新:

As mentioned in the code, the timer interval for each instance is set based on the config file values for corresponding element. 如代码中所述,每个实例的计时器间隔是根据相应元素的配置文件值设置的。 Right now, there are two sections in the config file: one has an interval of 15 seconds, and another one 10 seconds. 现在,配置文件中有两个部分:一个部分的间隔为15秒,另一部分的间隔为10秒。

Let me guess: The timer interval is 5min and the timer crashes causing the process to exit. 让我猜测:计时器间隔为5分钟,计时器崩溃,导致进程退出。

Why don't you log any crashes or attach a debugger? 您为什么不记录任何崩溃或附加调试器?

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

Mutex was getting instantiated every 5 minutes for some weird reason, and was setting _isNotRunning to true. 由于某种奇怪的原因,互斥体每5分钟实例化一次,并将_isNotRunning设置为true。 That was the issue. 那就是问题所在。

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

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