Win Command of the Command Line Interface - Debugging (
Page 3 of 7 )
Handling Debugging
One of the problems with console applications is that they execute and immediately exit. So, when you try to debug your application, you see a quick flash of the output and that’s it. Of course, you could place a breakpoint into the IDE, but that only helps when debugging from within the IDE. You might want a debug version of your utility to use with a batch file or script. The example application includes a special method called CheckDebug() that appears in Listing 2.
ADVERTISEMENT
Listing 2: Adding a Pause to the Debug Version
static void CheckDebug()
{
#if (DEBUG)
Console.Write("Press any key when ready...");
Console.ReadLine();
#endif
}
Visual Studio automatically creates a constant called DEBUG. You can set this constant on the Built tab of your project’s Properties dialog box as shown in Figure 2. The code shown in Listing 2 relies on the DEBUG constant. When you create a Debug build, the DEBUG constant is defined and the compiler adds the pause code into your application. When you create a Release build, the compiler doesn’t add the code, so the command line application works as normal.
Figure 2: The IDE adds both a DEBUG and TRACE define to your application by default.