我有一个由 Windows 服务调用的控制台应用程序。此控制台应用程序根据某些 App.config 文件条目创建 System.Timers.Timer 实例(我创建了一个自定义 App.config 部分,计时器实例的数量将与本部分中的元素相同)。控制台应用程序预计不会关闭 - 如果它因某种原因关闭,Windows 服务将再次调用它。
为了使控制台应用程序永远存在,我编写了一个无限循环作为控制台应用程序的最后一条语句。而(1 == 1){}。
问题是,我看到控制台应用程序每 5 分钟终止一次。我不明白为什么会这样。
如果有更好的方法,请提出建议。
代码
静态无效主要(字符串[] 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 秒。