0

Windows サービスによって呼び出されるコンソール アプリケーションがあります。このコンソール アプリケーションは、特定の App.config ファイル エントリに基づいて System.Timers.Timer のインスタンスを作成します (カスタムの App.config セクションを作成したので、タイマー インスタンスの数は、このセクションの要素の数と同じになります)。コンソール アプリケーションは閉じないことが想定されています。何らかの理由で閉じた場合、Windows サービスによって再度呼び出されます。

コンソール アプリケーションを永久に有効にするために、コンソール アプリケーションの最後のステートメントとして無限ループを記述しました。while (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; }
}        

アップデート:

コードで述べたように、各インスタンスのタイマー間隔は、対応する要素の構成ファイルの値に基づいて設定されます。現在、構成ファイルには 2 つのセクションがあります。1 つは 15 秒間隔で、もう 1 つは 10 秒間隔です。

4

3 に答える 3

3

推測させてください:タイマー間隔は5分で、タイマーがクラッシュしてプロセスが終了します。

クラッシュをログに記録したり、デバッガーをアタッチしたりしませんか?

于 2012-05-29T14:09:27.663 に答える
1

AppDomain.UnhandledException イベントを処理し、例外をログに記録します。

于 2012-05-29T14:20:28.533 に答える
0

Mutexは、奇妙な理由で5分ごとにインスタンス化され、_isNotRunningをtrueに設定していました。それが問題でした。

于 2012-07-03T04:54:53.423 に答える