写了一个windows服务程序,开机时自动启动服务写数据库,要求关机时能停止服务,因为关机时也要写数据库。但发现,每次就开机写了数据,关机没写,似乎关机没有停止服务或没执行服务里的OnStop()方法。
注:手动停止服务可以写入数据库。
有没有解决的方法啊!

解决方案 »

  1.   

    看看MS的帮助,可能对你有帮助:http://msdn.microsoft.com/zh-cn/library/system.serviceprocess.servicebase.onstop(VS.80).aspx
      

  2.   

    用windows中关机消息的api来执行
      

  3.   

    ServiceController sc = new ServiceController("");
      if (sc.Status == ServiceControllerStatus.Running)
      {
      sc.Stop();
      sc.WaitForStatus(ServiceControllerStatus.Stopped); //等待服务停止
      if (sc.Status == ServiceControllerStatus.Stopped)
      {
      sc.Start();
      }
      }
      else
      {
      sc.Start();
      }
      

  4.   

    重写OnPowerEvent和OnShutdown方法...在方法内处理...