Wix:如何强制终止进程/任务?

saf*_*jrz 6 wix wix-extension wix3.5

从使用 Wix 创建的 MSI 运行卸载时,我需要在尝试删除任何文件之前强制终止在后台运行的进程。主要应用程序由一个托盘图标组成,它反映了监视本地 Windows 服务的 bg 进程的状态(用 C# 制作,尽管这可能与进一步的相关性不大)。

我首先尝试了以下方法:

<File Id='FooEXE' Name='Foo.exe' Source='..\Source\bin\Release\Foo.exe' Vital='yes' />     
...
<InstallExecuteSequence>
  <Custom Action="CloseTray" Before="InstallValidate" />
</InstallExecuteSequence>
...
<CustomAction Id="CloseTray" ExeCommand="-exit" FileKey="FooEXE" Execute="immediate" Return="asyncWait" />
Run Code Online (Sandbox Code Playgroud)

确认应用程序关闭对话框后,托盘图标立即关闭,但卸载完成后,Foo.Exe 任务仍然出现在taskmgr 上。另外,还给出了以下错误消息:

错误消息#1

这就是为什么,然后我尝试了这个:

<InstallExecuteSequence>
  <Custom Action="Foo.TaskKill" Before="InstallValidate" />
</InstallExecuteSequence>
...
<CustomAction Id="Foo.TaskKill" Impersonate="yes" Return="asyncWait" Directory="WinDir" ExeCommand="\System32\taskkill.exe /F /IM Foo.exe /T" />
Run Code Online (Sandbox Code Playgroud)

获得相同结果后,尝试:

<Property Id="QtExecCmdLine" Value='"[WinDir]\System32\taskkill.exe" /F /IM Foo.exe'/>
...
<InstallExecuteSequence>
  <Custom Action="MyProcess.TaskKill" Before="InstallValidate" />
</InstallExecuteSequence>
...
<CustomAction Id="MyProcess.TaskKill" BinaryKey="WixCA" DllEntry="CAQuietExec" Execute="immediate" Return="ignore"/>
Run Code Online (Sandbox Code Playgroud)

我从这里获取的示例:How to Kill a process from WiX

最近,当其他一切都失败时,我也尝试过但没有成功:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
...
<InstallExecuteSequence>
 <Custom Action="WixCloseApplications" Before="InstallValidate" />
</InstallExecuteSequence>
...
<util:CloseApplication Id="CloseFoo" CloseMessage="yes" Description="Foo is still running!" ElevatedCloseMessage="yes" RebootPrompt="yes" Target="Foo.exe" />
Run Code Online (Sandbox Code Playgroud)

这给了我一个不同的错误:

错误#2,我在这里做错了什么?

我正在考虑建造一座雕像来纪念这个拒绝死亡的过程!...或者认为应用程序端存在问题,我应该在其中添加类似 Application.Exit(); 的内容 或环境.Exit(0); 在 Program.cs 内的某行。

我可以在 Wix 或我的应用程序中做任何其他事情来尝试在卸载时成功关闭它吗?谢谢!

cav*_*ick 3

就我个人而言,我认为您最好的选择是内置CloseApplication方法,而不是以前的选项。

您收到的错误(错误代码 2762)是因为您试图按立即顺序安排操作,但有将ElevatedCloseMessage="yes"其触发为延迟操作的设置。删除此属性或将其安排在延迟序列中。