pdiddy
pdiddy

Reputation: 6297

Reason why my application would not exit?

We have an application written in .net, c#, winforms. We noticed that sometimes when closing the application, the process remains.

I'm not sure how I can reproduce that behavior, so I'm looking for some clues as to why the application wouldn't exit.

The application uses a bit of background threads. Thread pools. Wondering if that could be the cause. Anything else could have this effect?

Upvotes: 3

Views: 186

Answers (5)

Shiv
Shiv

Reputation: 1404

If you are able to run in debug and you close the form, VS should not return to the normal code edit mode (it will still have the pause and stop buttons from debug active). You can then press Pause and check the Threads window to see where the call stacks are stuck for threads that are still active.

Upvotes: 0

Pontus Gagge
Pontus Gagge

Reputation: 17278

Get Process Explorer and possibly Process Monitor and see what thread is left suspended or running.

Upvotes: 1

Brian Gideon
Brian Gideon

Reputation: 48969

The application uses a bit of background threads. Thread pools. Wondering if that could be the cause. Anything else could have this effect?

It most definitely could be the cause though I cannot be certain that it actually is. One way to test this hypothesis to make sure all threads that have been explicitly created are designated as background threads. This can be done by setting Thread.IsBackground = true which will allow the application to terminate if the main thread ends. If there is at least one thread for which IsBackground = false then the CLR keeps the host process running.

Upvotes: 2

Stecya
Stecya

Reputation: 23276

If you have thread's that have IsBackground property set to false that are alive after application is closed, they will remain

Upvotes: 3

WraithNath
WraithNath

Reputation: 18013

Its likely to be a thread left running. If you have a look at the process in task manager you can see when a thread starts, and how many are left running when it exits by adding the 'Threads' column from the view menu.

I would start by making sure you start and end on the same thread count.

Upvotes: 1

Related Questions