【发布时间】:2012-05-25 17:03:42
【问题描述】:
安装我的服务后,我有一个处理程序,它会在安装后启动该服务。
private void InitializeComponent()
{
...
this.VDMServiceInstaller.AfterInstall += ServiceInstaller_AfterInstall;
}
private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController("MyService");
sc.Start();
}
我想在卸载之前停止服务,所以我向 InitializeComponent() 添加了一个额外的处理程序。
this.ServiceInstaller.BeforeUninstall += ServiceInstaller_BeforeUninstall;
并添加了功能:
private void ServiceInstaller_BeforeUninstall(object sender, InstallEventArgs e)
{
try
{
ServiceController sc = new ServiceController("MyService");
if (sc.CanStop)
{
sc.Stop();
sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
}
}
catch (Exception exception)
{}
}
但服务在卸载前不会停止。我是否不正确地使用了 ServiceController.Stop() 函数?
【问题讨论】:
-
8 年前问过,这仍然是一个问题。在我的例子中,我创建了用于创建另一个 Service2 的 Service1。 Service1 只是具有 API 的包装器,我可以调用这些 API 来启动 Service2。有趣的是,Service1 API 在卸载后将激活超过 2 分钟。我想完全停止服务然后卸载
标签: windows-services servicecontroller