我有一个Windows服务MyService(以C#实现)作为NetworkService运行。 MyService需要启动另一个也作为NetworkService运行的第三方服务ItsService。尝试使用该代码为MyService试图何时开始TheirService失败,拒绝访问错误:从另一个服务重新启动NetworkService服务
public static bool StartService(string serviceName, TimeSpan timeout)
{
bool started = false;
try
{
using (ServiceController sc = new ServiceController(serviceName))
{
if (sc != null)
{
sc.Refresh();
if (sc.Status == ServiceControllerStatus.Running)
{
// Stop service
Trace.TraceInformation("Util.StartService Stopping service '{0}'...", serviceName);
sc.Stop();
}
sc.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
sc.Refresh();
// Start service
Trace.TraceInformation("Util.StartService Starting service '{0}'...", serviceName);
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
sc.Refresh();
started = (sc.Status == ServiceControllerStatus.Running);
}
}
}
catch (Exception e)
{
Trace.TraceError("Util.StartService Exception occurred while starting service '{0}'.\n{1}\n{2}", serviceName, e.Message, e.StackTrace);
}
Trace.TraceInformation("Util.StartService Service '{0}' restarted? {1}", serviceName, started);
return started;
}
TheirService的SID信息:
C:\>sc showsid TheirService
NAME: TheirService
SERVICE SID: S-1-5-80-3034156332-2544749427-1608259134-1317875859-4063208518
有没有从为MyService从C#开始TheirService编程方式?
编辑:目标 - 作为MyService安装的一部分,我希望能够重新启动他们的服务。
他们的服务可以修改吗?如果是这样,你可以实现你想要的 – Learner 2012-07-26 21:48:22
@Cristi不幸的是不是。他们的服务是第三方服务。 – amo 2012-07-26 22:19:17