How to: Pause Code Execution At Runtime for Debugging?

joshuaand

Freshman
Joined
Sep 2, 2003
Messages
45
Location
Australia
Hi,

I am writing a little error Handler into all my subs and functions that will dump the error to a text file and continue, this is when the application is compiled and running on client machines.

However I dont want to do that when I am debugging, I want to go to the line and debug as I normally would.

So I have a couple of questions:

1.) Is there a way to tell if the application is running out of Visual Studio?
2.) Is there a way to pause the code when an error occurs:

eg:

Code:
Try
    'My Code Goes Here
Catch ex as exception
     if INVISUALSTUDIO then
           'Pause Execution HERE to Do SOme Stuff
     end if
     ProcessError(Ex.Source, Ex.StackTrace, Ex.Message)

end try
 
I think that there is a VB.NET Stop keyword that will send the execution to break mode in Visual Studio.
Visual Basic:
'The Stop keyword
Stop
 
You can always stop the execution of an app using the break mode of Visual Studio (normally a F9 key), there you can manage to see the values stored on variables, objects, threads, etc.
 
And why not? You can automatically insert a Stop statement with the Visual Studio Replace.
Replace:
ProcessError
with
Stop: ProcessError
and to revert it, replace:
Stop: ProcessError
with Process Error. :)
 
When compiling you can use the DEBUG compiler value. First you have to use the special If statement which is only read by the compiler. It looks like this:
Visual Basic:
#If DEBUG Then
     'do something if the program is compiled in DEBUG mode.
#End If
This kind of If statement will not be included in your program but rather the compiler will decide which code to compile based on the build configuration.
To apply it to your program you would have to use DEBUG build for testing and RELEASE for distribution (which is the setup you should be using :D). It would look like this:
Visual Basic:
Catch ex As Exception
     #If DEBUG Then
           Stop
     #Else
            'Process the error
     #End If
End Try
 
Last edited:
Back
Top