.netvb.netvisual-studioide

Conditional like: if application started from IDE then (do this) else (do nothing)


There is a way to execute a part of code ONLY if the application is launched from the IDE?.

I want to make a conditional where if application is launched from the VS IDE then do one thing but if application is launched manually from the compilation (from windows explorer, clicking in the compiled app, etc...) then do other thing.

This is possible?

Something like this, but changing #Debug to (Unknown thing...) :

Public Class Form2

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
#If DEBUG Then
        Me.Location = New Point(Form1.Right, Form1.Top)
#Else
        Dim BorderWidth = (Me.Width - Me.ClientSize.Width)
        Me.Location = New Point((Form1.Location.X + (Form1.Width + BorderWidth)), Form1.Location.Y)
#End If
    End Sub

End Class

Solution

  • What you really want to do is detect if there is a debugger attached to your application's process.

    To do that, test the value of the System.Diagnostics.Debugger.IsAttached property. If it returns true, then a debugger is attached.

    Of course, you can have a debugger other than Visual Studio attached to the process and that will still cause IsAttached to return true. And you can start an application from Visual Studio without the debugger attached (usually by pressing Ctrl+F5), and that will cause IsAttached to return false. But chances are good that whatever code you conditionally execute should run in all cases that a debugger is attached, and should not run whenever a debugger is not attached, regardless of which debugger it is.

    Note that this is different from #If DEBUG, since that tests whether you're running a Debug build of the application. Debug builds have nothing to do with whether a debugger is attached. You can have a Debug build without the debugger attached, or attach a debugger to the Release build.

    But, I really don't recommend using this for what you propose in the question. How will you debug problems with window placement if you're doing it differently each time a debugger is attached?