簡體   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