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?

Community
  • 1
  • 1
Ebikeneser
  • 2,582
  • 13
  • 57
  • 111
  • 2
    Most likely you have some threads still running after your main window closes. Do you use any `BackgroundWorker`s or `Thread`s? If so, you will have to somehow signal them to terminate. – Jon Jun 20 '12 at 09:55
  • 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? – Matthew Watson Jun 20 '12 at 09:58
  • I see, how can I tell the which threads are running and how to terminate them? – Ebikeneser Jun 20 '12 at 10:02

2 Answers2

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).

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
  • 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? – Ebikeneser Jun 20 '12 at 12:21
  • @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. – Roman Starkov Jun 20 '12 at 13:24
  • Can we do this without Visual Studio? – Imran Qadir Baksh - Baloch Sep 09 '14 at 06:30
  • 1
    @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. – Roman Starkov Sep 09 '14 at 18:19
  • @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 :) – iYadav Apr 26 '17 at 10:58
11

Environment.Exit(0);

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

0 is returned upon successful completion.

Robert
  • 2,222
  • 2
  • 21
  • 36