关机时停止windows服务 写了一个windows服务程序,开机时自动启动服务写数据库,要求关机时能停止服务,因为关机时也要写数据库。但发现,每次就开机写了数据,关机没写,似乎关机没有停止服务或没执行服务里的OnStop()方法。注:手动停止服务可以写入数据库。有没有解决的方法啊! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看MS的帮助,可能对你有帮助:http://msdn.microsoft.com/zh-cn/library/system.serviceprocess.servicebase.onstop(VS.80).aspx 用windows中关机消息的api来执行 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(); } 重写OnPowerEvent和OnShutdown方法...在方法内处理... 自定义控件在工具箱中的说明注释 错误列表问题? xml文件反序列化的问题? 求一条语句:C#界面跳转问题。在线等 可以用类作为声明数组的类型吗 listview列表文件夹下的图片文件,帮忙啊大哥们 关于C# 3.0 学习指南一书 100分求微软的一个采用c#语言写的VSIDE 源码 C#开发C/S软件的架构问题 在自定义工具箱中添加 MSComm32.Ocx控件,为何出现“没有使用该控件的许可证” 请问FireWorks 有C# 能用的API吗? 点击树菜单的一个子菜单,插入时间……
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped); //等待服务停止
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
}
else
{
sc.Start();
}