Reputation: 1670
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
Reputation: 18320
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:
Right-click your project in the Solution Explorer
and press Properties
.
Go to the Application
tab.
Press View Application Events
.
Select MyApplication (events)
in the left combo box.
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
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:
Open Regedit.
Navigate to the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
key.
Locate the key with the name of your application (if it exists), or create a new one in the format of yourApplicationName.exe
.
Create a new String
value (REG_SZ
) and name it debugger
.
Set the value of debugger
to vsjitdebugger.exe
.
Go ahead and start debugging!
For more information see: How to: Launch the Debugger Automatically - MSDN
Upvotes: 1