C#服务安装程序 - 卸载前升级安装不停止服务

时间:2016-06-10 17:59:02

标签: c# service installshield

请查看以下代码,了解我的项目安装程序BeforeInstall事件。正在调用该事件,因为安装成功完成并将卸载我的服务,然后再安装新服务,但仅限于在运行新版本的安装程序之前停止正在运行的服务。

由于某种原因,事件中的代码要么没有停止服务,要么就是没有等待服务停止,这两个我都告诉它在继续卸载服务之前要做的 - 这又是如果服务未运行,将成功完成。它似乎甚至没有停止服务,因为在我收到资源正在使用的错误后,我查看服务并看到该服务确实仍在运行。作为参考,我还包括成功启动我的服务的AfterInstall事件。

[编辑] 在我的事件触发之前,似乎检查是否正在使用资源。实际上,我已经尝试过安装程序和服务安装程序的多个事件无济于事。比如OnBeforeUninstall ...等...我尝试了多种关闭进程的方法,包括打开cmd.exe并在我的服务上运行sc stop。在检查资源是否正在使用之前,它甚至不会尝试运行cmd.exe。在资源检查发生之前,是否有更好的地方运行服务停止代码?我甚至在项目安装程序类的Initialize组件之前抛出它。有谁知道资源检查何时发生?

void ProjectInstaller_BeforeInstall(object sender, InstallEventArgs e)
    {
        List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices());

        foreach (ServiceController s in services)
        {
            if (s.ServiceName == this.serviceInstaller1.ServiceName)
            {
                if(s.Status == ServiceControllerStatus.Running)
                {
                    s.Stop();
                    s.WaitForStatus(ServiceControllerStatus.Stopped);
                }

                ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
                ServiceInstallerObj.Context = new InstallContext();
                ServiceInstallerObj.Context = Context;
                ServiceInstallerObj.ServiceName = s.ServiceName;
                ServiceInstallerObj.Uninstall(null);

                break;
            }
        }
    }

private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
    {
        using (ServiceController s = new ServiceController(serviceInstaller1.ServiceName))
        {
            s.Start();
        }
    }

0 个答案:

没有答案
相关问题