申请流程不会关闭

问题描述 投票:0回答:3

我有一个WPF应用程序,关闭应用程序后,其进程

app.exe *32
仍在任务管理器的进程列表中运行。

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

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.

我知道以前曾在这里问过此类问题。

但是,通过将我的

Assembly.cs

更改为-
,该解决方案对我不起作用

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

我想也许如果我要找到

Window closed

 事件并在事件中放置类似 - 
Process.GetCurrentProcess().Kill();
 的内容,这样当用户通过表单右上角的红色 
'x'
 按钮关闭应用程序时,就会出现这样的情况也许杀死进程? 

c# wpf process exe
3个回答
49
投票
因此,您的进程在关闭后仍然存在。这通常意味着您有一个线程可以使其保持活动状态。您可以通过以下方式追踪它。

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

                             

enter image description here

enter image description here

                                       

enter image description here

现在打开主题列表:

      

enter image description here

双击您在此处看到的

每个线程:

enter image description here

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

                             

enter image description here

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

Thread.IsBackground

 设置为 
true
)。


11
投票
环境.退出(0);

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

成功完成后返回0。


0
投票
如果您在 2023 年使用 Visual Studio 进行开发,您可以可视化线程/任务:

    明智地选择一些断点,引导您完成应用程序
  1. 开始调试
  2. 选择“调试”>“窗口”>“并行堆栈” 这很好地显示了哪些任务正在(仍在)运行
  3. 现在单步执行代码,看看哪里卡住了。
© www.soinside.com 2019 - 2024. All rights reserved.