私のサービスがインストールされると、インストール後にサービスを開始するハンドラーがあります。
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()関数を不適切に使用していますか?