如何在 C# 中停止进程,知道它的文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9681875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop process in C#, knowing its filename?
提问by Pavel Oganesyan
I have a programm that runs another one (lets call the first app Stater and the second app - Worker).
我有一个运行另一个程序的程序(让我们调用第一个应用程序 Stater 和第二个应用程序 - Worker)。
I use
我用
process.start();
process.waiForExit();
process.Close();
in Starter.
在启动器中。
But if Starter is forced to close while waiting for Worker (for some extern reason) Worker will be still in processes, blocking files, eating memory etc.
但是如果 Starter 在等待 Worker 时被迫关闭(由于某些外部原因),Worker 将仍在进程中,阻塞文件,占用内存等。
So, I want to check if Worker is already running before I will try to start it. I've tried Process.GetProcessesByName("worker.exe") but no luck (even if I can see Worker in Task Manager).
所以,我想在尝试启动它之前检查 Worker 是否已经在运行。我试过 Process.GetProcessesByName("worker.exe") 但没有运气(即使我可以在任务管理器中看到 Worker)。
I've seen some topics here about checking every process in memory for its modules, but still I already know the running file I hope to avoid such solution.
我在这里看到了一些关于检查内存中每个进程的模块的主题,但我仍然知道我希望避免这种解决方案的运行文件。
Any advices?
有什么建议吗?
采纳答案by Coder
At the time of starting Worker process, save its ID in your application's configuration/setting file, in this way when you will launch your Starter process, it will first load that ID from settings file and will check if that process is currently running or not. If you want to immediately close Worker process, you can call process.Kill()on it.
在启动 Worker 进程时,将其 ID 保存在应用程序的配置/设置文件中,这样当您启动 Starter 进程时,它会首先从设置文件中加载该 ID 并检查该进程当前是否正在运行. 如果你想立即关闭 Worker 进程,你可以调用process.Kill()它。
回答by Tung
The reason you cannot find it is because you're using .exe. If the executable shows up as worker.exe in TaskManager, just call:
您找不到它的原因是因为您使用的是 .exe。如果可执行文件在 TaskManager 中显示为 worker.exe,只需调用:
Process[] workers = Process.GetProcessesByName("worker")
foreach (Process worker in workers)
{
worker.Kill();
worker.WaitForExit();
worker.Dispose();
}
回答by Steve
When you call GetProcessesByName("worker")you don't specify exe extension as explained in MSDN
And if you wish to keep a global variable with the process object that you have started you could simply use the process.Kill();
当您调用GetProcessesByName("worker") 时,您没有按照MSDN 中的说明指定 exe 扩展名
如果您希望使用已启动的进程对象保留全局变量,您可以简单地使用process.Kill();
回答by Jon
If you only need to detectthat "worker" is running, a technically much superior solution is have it lock a global mutexfor the duration of its lifetime. Any process that knows the mutex name (which is under your control) can then see if the mutex is locked (if it is, worker is running).
如果您只需要检测“worker”正在运行,一个技术上更优越的解决方案是让它在其生命周期内锁定全局互斥锁。任何知道互斥锁名称(在您的控制之下)的进程都可以查看互斥锁是否被锁定(如果是,则工作线程正在运行)。
However this is not easy to implement correctlyas there are many little details you want to get just right; even then, there might be a race condition of "starter" and "worker" are actually launched simultaneously etc (of course this problem, and many others, also apply to all other solutions so it cannot be considered a drawback).
然而,这并不容易正确实施,因为您需要正确处理许多小细节;即便如此,也可能存在“starter”和“worker”实际上同时启动等的竞争条件(当然,这个问题和许多其他问题也适用于所有其他解决方案,因此不能被视为缺点)。
回答by Roshana Pitigala
This is much easier...
这要容易得多...
System.Diagnostics.Process.Start("cmd.exe","/c taskkill /IM notepad.exe");
This code will close the Notepad (if it is running). Type the program name you want to close with it's extension (.exe).
此代码将关闭记事本(如果它正在运行)。键入要关闭的程序名称及其扩展名 (.exe)。
Some applications cannot be stopped without forcing.
Use /Fafter taskkillto force the action.
If you want to close the process tree use /Tafter program name.
某些应用程序无法在不强制的情况下停止。使用/Faftertaskkill强制操作。如果要关闭进程树,请/T在程序名称后使用。
System.Diagnostics.Process.Start("cmd.exe","/c taskkill /F /IM notepad.exe /T");

