C#如何使用服务定时启动服务?

bullion 2009-09-11 03:34:46
我现在有服务A, 服务B, 服务C

我想用服务A定时去检测 服务B 和 服务C, 发现服务停止即重新开启服务。

不知道有没有办法可以做到?
...全文
260 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2009-09-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 bullion 的回复:]
...
我想用服务A定时去检测 服务B 和 服务C, 发现服务停止即重新开启服务。
...
[/Quote]

建议不要这样做。

1、服务作为长时间运行的程序,本身应该是健壮的。用守护进程的方式来解决服务不稳定,只会带来新的问题。

2、对于关键服务,Windows已经提供了恢复功能,允许在服务出错的情况下重启服务,甚至重启系统。
飞天鹰 2009-09-11
  • 打赏
  • 举报
回复
在程序里添加一个timer控件,然后在该控件的属性的Interval设置每隔多长时间执行一次,这里的单位是毫秒,然后双击timer控件,在里面写代码:检测有没有服务B和C的进程,有的话就什么都不执行,没有的话就用Process.Start("服务名")启动起来。关于进程的操作,可以参考下列文章:http://blog.csai.cn/user1/22158/archives/2007/16631.html
李冬宝 2009-09-11
  • 打赏
  • 举报
回复
                    ServiceController sc = new ServiceController("servicename"); 
ServiceControllerStatus ls = sc.Status;
switch (sStatus)
{
case "Stopped":
if (ls == ServiceControllerStatus.Running || ls == ServiceControllerStatus.Paused)
{
sc.Stop();
}
break;
case "Paused": if (ls == ServiceControllerStatus.Running)
{
sc.Pause();
}
break;
case "Running": if (ls == ServiceControllerStatus.Stopped)
{
sc.Start();
}
else if (ls == ServiceControllerStatus.Paused)
{
sc.Continue();
}
break;
}
李冬宝 2009-09-11
  • 打赏
  • 举报
回复
ServiceController sc = new ServiceController("servicename");
ServiceControllerStatus ls = sc.Status;
switch (sStatus)
{
case "Stopped":
if (ls == ServiceControllerStatus.Running || ls == ServiceControllerStatus.Paused)
{
sc.Stop();
}
break;
case "Paused": if (ls == ServiceControllerStatus.Running)
{
sc.Pause();
}
break;
case "Running": if (ls == ServiceControllerStatus.Stopped)
{
sc.Start();
}
else if (ls == ServiceControllerStatus.Paused)
{
sc.Continue();
}
break;
}
柳晛 2009-09-11
  • 打赏
  • 举报
回复
检测进程,使用提示符命令重启服务。
bullion 2009-09-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lzsh0622 的回复:]
A服务中,加定时器timer控件,Tick事件中检测服务B、服务C是否运行,没运行启动;


[/Quote]

能不能提供代码???
lzsh0622 2009-09-11
  • 打赏
  • 举报
回复
A服务中,加定时器timer控件,Tick事件中检测服务B、服务C是否运行,没运行启动;

enihs 2009-09-11
  • 打赏
  • 举报
回复
批处理做的事
lanruoshui 2009-09-11
  • 打赏
  • 举报
回复
关注~
tsp860901 2009-09-11
  • 打赏
  • 举报
回复
顶,,

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧