本文介绍了ManagedInstallerClass.InstallHelper 正在锁定 WinService exe 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下(简化的)安装 WinService 的代码:

I have following (simplified) code for installing WinService:

    public static bool InstallService(string fullFileName)
    {
        try
        {
            ManagedInstallerClass.InstallHelper(new[] { fullFileName });
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

我注意到在我的安装向导中调用它时,WinService 的 EXE 文件被锁定,直到整个安装向导未完成.有什么办法可以避免这个锁吗?InstallService 完成后如何释放资源"?我在此处发现了类似的问题.GC.Collect() 没有帮助我.

I noticed that when calling it in my installation wizard, EXE file of an WinService is locked until whole installation wizard is not finished. Is there any way how to avoid this lock? How to "free resources" just after InstallService is done? I found similar issue here. GC.Collect() did not help me.

我试图在单独的线程中调用方法,但没有成功.

I tried to call method in separate thread, but without success.

推荐答案

根据一些研究,我找到了不锁定服务可执行文件的解决方案.

Based on some research I found solution which does not lock service's executable.

public static bool InstallService(string fullFileName)
    {
        try
        {
            using (var ai = new AssemblyInstaller(fullFileName, null))
            {
                ai.Install(null);
                ai.Commit(null);
                return true;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }

这篇关于ManagedInstallerClass.InstallHelper 正在锁定 WinService exe 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 05:24