Process.Close() 不会终止创建的进程,c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673031/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:49:17  来源:igfitidea点击:

Process.Close() is not terminating created process,c#

c#process

提问by Anirudh Goel

I've written a C# application which uses System.Diagnostics.Processclass to create a process, using

我写了一个 C# 应用程序,它使用System.Diagnostics.Process类来创建一个进程,使用

 Process P1 = new Process();
 P1.FileName = "myexe.exe";

and other proper settings.

和其他适当的设置。

I've linked it to an exe file which runs for about 10 minutes. (I'm writing program to measure run-time of programs). Now in between I want to abort the running process. So I wrote in the cancel button's event,

我已将它链接到一个运行大约 10 分钟的 exe 文件。(我正在编写程序来测量程序的运行时间)。现在,我想中止正在运行的进程。所以我在取消按钮的事件中写道,

 Process.Close();

But in the task manager I still see myexe.exe running, it doesn't get aborted. What to do?

但是在任务管理器中我仍然看到 myexe.exe 正在运行,它没有被中止。该怎么办?

采纳答案by Jon Skeet

Process.Close()isn't meant to abort the process - it's just meant to release your "local" view on the process, and associated resources.

Process.Close()并不是要中止进程——它只是为了释放你对进程和相关资源的“本地”视图。

I think you mean Process.Kill()or Process.CloseMainWindow(). Personally I'd try to find a more graceful way of shutting it down though.

我想你的意思是Process.Kill()Process.CloseMainWindow()。就我个人而言,我会尝试找到一种更优雅的方式来关闭它。

回答by Dave Van den Eynde

Use Process.Killinstead.

请改用Process.Kill

回答by meatvest

I think Process.Kill()is what you're looking for.

我想Process.Kill()这就是你要找的。