开发者

Reason why my application would not exit?

开发者 https://www.devze.com 2023-03-13 23:43 出处:网络
We have an application written in .net, c#, winforms. We noticed that sometimes when closing the application, the process remains.

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 bac开发者_如何学Pythonkground threads. Thread pools. Wondering if that could be the cause. Anything else could have this effect?


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


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消