thebunnyrules
thebunnyrules

Reputation: 1670

How do I debug across application restarts in Visual Studio?

I'm building a Visual Basic application in Visual Studio 2010. Some of my options can only be applied on restarting the application.

I have not gotten a single crash when application is running normally. Nor does it crash when I apply settings, manually exit and restart. On the other hand, as soon as I try to do a automatic restart from the application, I get an exception on one out of 5-10 restarts.

I've tried to run the debugger, but as soon as the application restarts, the Visual Studio debugger is turned off and does not turn back on when the application launches again. Nor does it launch again with same configurations. It seems the debugger launched application configuration and the manually launched application configuration files are different.

Is there a way I can get around this? Keep the debugger on across restarts? Or should I undertake a different strategy?

Upvotes: 3

Views: 1834

Answers (1)

Visual Vincent
Visual Vincent

Reputation: 18320

Method 1: Attaching the debugger from within the application

If the application sometimes crashes at startup add a call to Debugger.Launch() in the application's Startup event. Doing so will cause Visual Studio to open a window where you can choose to attach its debugger.

You can check the Debugger.IsAttached property in order to determine whether a debugger is already attached or not.

Steps to subscribe to the Startup event:

  1. Right-click your project in the Solution Explorer and press Properties.

  2. Go to the Application tab.

  3. Press View Application Events.

  4. Select MyApplication (events) in the left combo box.

  5. Select Startup in the right combo box.

Code:

Imports System.Diagnostics

...

Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    If Debugger.IsAttached = False Then
        Debugger.Launch()
    End If
End Sub

Method 2: Letting Windows start the debugger, which in turn starts your application

An alternative solution is to add your application to the Image File Execution Options registry key, which allows you to specify a debugger which should launch the application for you.

NOTE: Adding your application to Image File Execution Options causes Windows to automatically launch the specified debugger instead of your application whenever you try to open it. Your application's path is passed as a command line argument and it is then up to the debugger to launch your application, attaching itself to it.

Malwarebytes have some info about Image File Execution Options on their blog: An introduction to Image File Execution Options.

Here's how you'd do it:

  1. Open Regedit.

  2. Navigate to the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options key.

  3. Locate the key with the name of your application (if it exists), or create a new one in the format of yourApplicationName.exe.

  4. Create a new String value (REG_SZ) and name it debugger.

  5. Set the value of debugger to vsjitdebugger.exe.

  6. Go ahead and start debugging!

For more information see: How to: Launch the Debugger Automatically - MSDN

Upvotes: 1

Related Questions