我有以下(简化)安装WinService的代码:
public static bool InstallService(string fullFileName)
{
try
{
ManagedInstallerClass.InstallHelper(new[] { fullFileName });
return true;
}
catch (Exception ex)
{
return false;
}
}
我注意到在我的安装向导中调用它时,WinService的EXE文件被锁定,直到完成整个安装向导。有什么方法可以避免这种锁定吗?如何"免费资源"在InstallService完成之后?我发现了类似问题here。 GC.Collect()没有帮助我。
我试图在单独的线程中调用方法,但没有成功。
答案 0 :(得分:0)
根据一些研究,我找到了不会锁定服务可执行文件的解决方案。
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;
}
}