如何在C#编写的服务程序中管理(启动、暂停、停止)其他系统服务?这是我写的程序:
protected override void OnStart(string[] args)
{
Thread Th = new Thread(this.Running);
Th.Start(); }
private void Running()
{
sc=new System.ServiceProcess.ServiceController("W3SVC");
while (true)
{
try
{
TcpClient tc = new TcpClient();
tc.Connect("localhost", 80);
tc.Close();
tc = null;
}
catch
{
string scStatus = sc.Status.ToString();
switch (scStatus.ToUpper())
{
case "PAUSED":
sc.Continue();
break;
case "STOPPED":
sc.Start(); // VS显示问题出在这里
break;
}
scStatus = null;
}
Thread.Sleep(3 * 1000);
}
}
当W3SVC服务停止时,VS调试程序却总是提示“无法启动.上的W3SVC服务(大概是这个意思)”
protected override void OnStart(string[] args)
{
Thread Th = new Thread(this.Running);
Th.Start(); }
private void Running()
{
sc=new System.ServiceProcess.ServiceController("W3SVC");
while (true)
{
try
{
TcpClient tc = new TcpClient();
tc.Connect("localhost", 80);
tc.Close();
tc = null;
}
catch
{
string scStatus = sc.Status.ToString();
switch (scStatus.ToUpper())
{
case "PAUSED":
sc.Continue();
break;
case "STOPPED":
sc.Start(); // VS显示问题出在这里
break;
}
scStatus = null;
}
Thread.Sleep(3 * 1000);
}
}
当W3SVC服务停止时,VS调试程序却总是提示“无法启动.上的W3SVC服务(大概是这个意思)”
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货