日期:2014-05-18  浏览次数:20733 次

windows服务项目,运行一段时间自动停止。找不到原因。
在配置文件中设置了,按天和按小时。
按小时,跑了几个小时都没啥问题。按天,早上4点钟运行。竟然提前服务停止了。。打不到原因。也不知道如何查原因。
代码如下:
 protected override void OnStart(string[] args)
  {
  // TODO: 在此处添加代码以启动服务。
  CDataLog.WriteValueToFile(Global.fileLog, "-------开始定时任务------"+DateTime.Now.ToString());
  try
  {
  Thread t = new Thread(new ThreadStart(TimeTask));
  t.Start();
  CDataLog.WriteValueToFile(Global.fileLog, "-------开始进程------" + DateTime.Now.ToString());
   
  }
  catch (Exception ex)
  {
  CDataLog.WriteValueToFile(Global.fileLog, ex.ToString());
  }
  CDataLog.WriteValueToFile(Global.fileLog, "-------结束定时任务------" + DateTime.Now.ToString());

   
  }

  protected override void OnStop()
  {
  // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
  //timer.Stop();
  try
  {
  CDataLog.WriteValueToFile(Global.fileLog, "-------结束服务------" + DateTime.Now.ToString());
  }
  catch (Exception ex)
  {
  CDataLog.WriteValueToFile(Global.fileLog, "结束任务"+ex.ToString());
  }
  }

//
 private void TimeTask()
  {
  while (true)
  {
  AppConfig config = new AppConfig();
  //不推送的日期为1日。
  int notPushTime = Convert.ToInt32(config.getNotPushTime());
  // DateTime d1 = new DateTime(now.Year, now.Month, 1);


  //按天,startTime:当天-1,endTime:当天 
  //按小时,startTime:当前-1小时,endTime:当前小时0分。
  // 推送状态 
   
  int hour = DateTime.Now.Hour;
  DateTime notPushDt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, notPushTime);
  string cycle = config.getCycleTimeByConfig();
  string tempDt = DateTime.Now.ToString("yyyy-MM-dd");
  DateTime startTime;
  DateTime endTime;
  //按天,startTime:当天-1,endTime:当天 
  //按小时,startTime:当前-1小时,endTime:当前小时0分。
   
  if (string.Equals(cycle, "day"))
  {
  endTime = DateTime.Parse(tempDt);
  startTime = DateTime.Parse(tempDt).AddDays(-1);
  CDataLog.WriteValueToFile(Global.fileLog, "---按天"+DateTime.Now.ToString()+"----");
  }
  else
  {
  endTime = DateTime.Parse(tempDt + " " + DateTime.Now.Hour.ToString() + ":00 ");
  startTime = endTime.AddHours(-1);
  CDataLog.WriteValueToFile(Global.fileLog, "---按小时"+DateTime.Now.ToString()+"----");
  }
  BussinessData bussisData = new BussinessData();
  double inteval = double.Parse(new AppConfig().getInterval());
  if (notPushDt.CompareTo(Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd"))) != 0)