Application process will not close
Asked Answered
E

3

13

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

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.cs to -

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

I thought that perhaps if I were to find the Window closed event 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?

Eustace answered 20/6, 2012 at 9:54 Comment(3)
Most likely you have some threads still running after your main window closes. Do you use any BackgroundWorkers or Threads? If so, you will have to somehow signal them to terminate.Crate
There are two categories of threads: Background and non-background. Background threads will automatically be terminated when the parent process dies; non-background threads will keep the parent process alive. Perhaps you have one or more threads that should be background threads?Cantaloupe
I see, how can I tell the which threads are running and how to terminate them?Eustace
S
49

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 each thread 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.IsBackground to true).

Smoothen answered 20/6, 2012 at 11:40 Comment(5)
This is a very useful answer, and now I can see the threads, however the source is not available as it is in your app?Eustace
@Jambo But you can see the thread that's keeping it alive, right? Does it have a name? What's the category? You might want to post a screenshot of your threads in your question.Smoothen
Can we do this without Visual Studio?Palaeozoic
@user960567 you should be able to do this with any debugger worth its salt, but I only know how to do this in Visual Studio. WinDbg can certainly do this, but it's not exactly user-friendly.Smoothen
@RomanStarkov Very helpful. Helped me track down similar thread issue which was trying to check internet availability every 60000 ms. Tracking that thread and setting Thread.IsBackground to true resolved my issue. Thanks a lot :)Sherilyn
V
11

Environment.Exit(0);

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

0 is returned upon successful completion.

Varicella answered 20/6, 2012 at 9:59 Comment(0)
F
0

If you are developing with Visual Studio in 2023, you can visualize the threads/tasks:

  1. Wisely choose some breakpoints that will lead you through the wrapping up of the application
  2. Start debug
  3. Select Debug > Windows > Parallel Stacks This shows nicely which tasks are (still) running
  4. Now step through the code and see where you get stuck.
Franz answered 23/10, 2023 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.