逻辑

2012-07-16 99 views
-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和实现逻辑,请指教如何执行。

+0

的文档你的问题是过高的水平,人们可能会发现很难在这里你需要有更好的问题回答合乎逻辑的问题。 – 2012-07-16 11:44:34

回答

1

试试这个。检查AppSettings

var yourAppKey = ConfigurationManager.AppSettings.Get("RUN_NOW"); 
         or 
var yourAppKey = ConfigurationManager.AppSettings["RUN_NOW"]; 
// do something with yourAppKey 
+0

如果未设置配置值,'ToString'方法是否会引发异常? – Kane 2012-07-16 11:44:58

+0

不需要ToString作为AppSettings.Get返回String。访问项目的另一种方式是AppSettings [“Run_Now”] – ZafarYousafi 2012-07-16 11:47:02

+0

@ravi我知道如何从app.config获取代码后面的代码,我想知道上面的条件逻辑 – user1494292 2012-07-16 12:21:10