C# 申请过程不会关闭

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

Application process will not close

c#wpfprocessexe

提问by Ebikeneser

I have a WPF application, after closing the app its process app.exe *32is still running in the processes list in task manager.

我有一个 WPF 应用程序,关闭应用程序后,它的进程app.exe *32仍在任务管理器的进程列表中运行。

I need this to close as when I make an edit to my code I get the following error -

我需要关闭它,因为当我对代码进行编辑时,出现以下错误 -

Unable to copy file "obj\x86\Release\frontEndTest.exe" to "bin\Release\app.exe". The process cannot access the file 'bin\Release\app.exe' because it is being used by another process.

I am aware that this sort of question has been asked before here.

我知道这里之前已经问过这种问题

However the solution did not work for me by changing my Assembly.csto -

但是,通过将我的更改Assembly.cs为 -解决方案对我不起作用

[assembly: AssemblyVersion("2.0.0")]
[assembly: AssemblyFileVersion("2.0.0")]

I thought that perhaps if I were to find the Window closedevent and puttting something like - Process.GetCurrentProcess().Kill();in the event so that when a user closed the application from the red 'x'button in the top right of the form this would perhaps kill the process?

我想,也许如果我要找到该Window closed事件并放置类似的东西 -Process.GetCurrentProcess().Kill();在该事件中,当用户从'x'表单右上角的红色按钮关闭应用程序时,这可能会终止该进程?

采纳答案by Roman Starkov

So your process is still alive after you've shut it down. This usually means you have a thread that keeps it alive. Here's how you can track it down.

所以你的进程在你关闭之后仍然存在。这通常意味着您有一个线程可以使其保持活动状态。以下是您可以追踪它的方法。

First, attach to it in the debugger:

首先,在调试器中附加到它:

                                enter image description here

                                在此处输入图片说明

enter image description here

在此处输入图片说明

                                        enter image description here

                                        在此处输入图片说明

Now open the threads list:

现在打开线程列表:

        enter image description here

        在此处输入图片说明

Double-click eachthread you see here:

双击您在此处看到的每个线程:

enter image description here

在此处输入图片说明

And you'll be taken to what the thread is currently doing:

您将被带到线程当前正在执行的操作:

                                enter image description here

                                在此处输入图片说明

That, right there, is what preventing the app from shutting down. One would have to exit that loop for the app to exit (or, alternatively, set Thread.IsBackgroundto true).

这就是阻止应用程序关闭的原因。必须退出该循环才能让应用退出(或者,设置Thread.IsBackgroundtrue)。

回答by Robert Northard

Environment.Exit(0);

环境.退出(0);

Terminates this process and gives the underlying operating system the specified exit code.    

0 is returned upon successful completion.

成功完成后返回 0。