1
votes

In the Visual Studio environment, I noticed two different end behaviors in my program while debugging. When I would press the Stop Debugging button while my program is still running I experienced a different behavior than when I actually close the form by clicking on the top right red X which also stops debugging. I always thought they did the same thing, but for my program, I am experiencing different end conditions. Can anyone tell me if there really is a difference between these two methods of ending debugging.

Stop Debugging Button

Closing the Form

2

2 Answers

4
votes

Sure, big difference. Clicking the windows' Close button starts a controlled shutdown of your app, starting with the FormClosing event getting fired. If it is the main window of your app, Application.Run() returns in your Main() method, the Main() method returns and that ends your UI thread. If there are no more non-background threads left then the CLR shuts down in a controlled way, running all remaining finalizers, unloading the AppDomain and ending the process.

Using the debugger's Stop Debugging command is an instant rude process abort. No code at all runs, neither in your program or the CLR. Bam! over.

1
votes

Closing the form will follow any or all closing events. Stopping the debugger terminates the program immediately.