Tuesday, August 10, 2004 - Posts

'Stop Debugging' option in VS.NET does not stops code execution

Behavior
There are cases, when program execution continues after 'Stop debugging' option (menu or toolbar) selected in Visual Studio .NET. 

Steps to reproduce behavior
1. Create default ASP.NET VB application
2. Add WebForm
3. Add Button to WebForm
4. Add following code to WebForm (you will, probably, have to update connection string):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                    Handles Button1.Click
   Dim cn As New SqlClient.SqlConnection("server=.;uid=sa;pwd=;initial catalog=Northwind;")
   cn.Open()
   Dim cmd As New SqlClient.SqlCommand("update categories set categoryname=categoryname+'1' where categoryid=1", cn)
   cmd.ExecuteNonQuery()
End Sub
5. Set breakpoint on first line of  Button1_Click and run the program.
6. Hit 'Stop Debugging' button immediately after debugger reach breakpoint.
7. Inspect your database. In most cases it will be updated!

Explanation
Turns out that VS.NET debugger does not suppose to halt managed process execution when you 'Stop Debugging'. It is just detaches from process. With unmanaged code, this requires killing process.

Solution
If you will check "Unmanaged code debugging' option in Project>Properties>Configuration Properties>Debugging, execution will stop immediately, when 'Stop Debugging' selected.