-3

在使用 c# 的窗口服务时,我想根据我的配置标签运行服务,实际上我将在 app.config 中设置值的三个标签,如下所述

<add key ="FIREHOST_TIME" value ="5" ></add>
<add key ="SETDAYS" value ="3" ></add>
<add key ="RUN_NOW" value ="1" ></add> <!-- 0=no, 1=yes-->

如果假设 RUN_NOW 值为 1,

当服务启动时,它现在必须工作,下一个实例应该基于 SETDAYS 标签运行。

如果假设 RUN_NOW 值为 0,

当服务启动时,它不应该做它应该等待 SETDAYS 到来的工作,然后下一个实例应该为每个设置的天标签运行。

下面我通过代码粘贴:

   protected override void OnStart(string[] args)
    {
     DateTime tenAM = DateTime.Today.AddHours(FIREHOST_TIME);



            if (DateTime.Now > tenAM)
                tenAM = tenAM.AddDays(SETDAYS);


            // calculate milliseconds until the next 10:00 AM.  
            int timeToFirstExecution = (int)tenAM.Subtract(DateTime.Now).TotalMilliseconds;

            // calculate the number of milliseconds in 24 hours.   
            int timeBetweenCalls = (int)new TimeSpan(24, 0, 0).TotalMilliseconds;

            TimerCallback methodToExecute = kickstart;

            // start the timer.  The timer will execute "ProcessFile" when the number of seconds between now and   
            // the next 10:00 AM elapse.  After that, it will execute every 24 hours.   
            System.Threading.Timer timer = new System.Threading.Timer(methodToExecute, null, timeToFirstExecution, timeBetweenCalls);


         }

所以现在我必须使用标签帮助 RUN_NOW 并实现逻辑,请建议如何执行。

4

1 回答 1

1

试试这样。检查AppSettings的文档

var yourAppKey = ConfigurationManager.AppSettings.Get("RUN_NOW"); 
                      or 
var yourAppKey = ConfigurationManager.AppSettings["RUN_NOW"]; 
 // do something with yourAppKey
于 2012-07-16T11:43:11.010 回答