TaskKill for killing .NET console application

2019-09-03 03:53发布

I have a bunch of console host applications hosting WCF services. I'm calling a .bat file as a pre build step to kill any running host instances so that I don't get WCF channel registrations errors(manually killing the console hosts each time before a build is a royal pain).

The .bat file I've created contains the following.

taskkill /T /F /FI "imagename eq Host.vshost.exe"
taskkill /T /F /FI "imagename eq Host.exe"
exit /B 0

This kills both processes. I can see in taskmanager that Host.exe is gone and Host.vshost.exe has a new PID but the Console Window is still up. It seems that cmd.exe is the actual process hosting the console so I then changed the .bat file to this..

taskkill /T /F /FI "imagename eq cmd.exe"

But this kills all cmd.exe windows.

How can I tweak this so that i can target just the specific Console application's cmd window or is there a different command I should be using?

1条回答
Melony?
2楼-- · 2019-09-03 04:11

taskkill has several options to help you narrow down the process you want to kill. For example, is the window title of the CMD.EXE process you want to kill unique (e.g. "Batch Processing" instead of the default "C:\WINDOWS\System32\cmd.exe")? Then use

taskkill /F /T /FI "WINDOWTITLE eq Batch Processing"

If you need to change the title of the target window, you can do so from the target batch file with the command TITLE Your Title Here, or by starting that process with a start "Your Title Here" ... command.

查看更多
登录 后发表回答