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 秒間隔です。