当我的服务安装时,我有一个处理程序在安装后启动服务。卸载时自动停止Windows服务
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()函数?
不起作用。它几乎就像该函数不会被调用,因为该函数也应该引发BeforeUninstall事件。 –