Starting the Visual Studio debugger when Attach to Process doesn’t work

Problem

You’re trying to debug a program with Visual Studio but Attach to Process does not work.

Most likely you’re loading your code through some third-party process (like Excel), and when you try to use Attach to Process it simply doesn’t work.

Solution

Instead of trying to use Attach to Process, you can launch a debugger instance from your code by calling System.Diagnostics.Debugger.Launch().

Here’s how to do that:

1. Add the call to System.Diagnostics.Debugger.Launch()

public class StoreRepository : List<Store> { private const double METERS_PER_MILE = 1609.344; public IEnumerable<Store> GetStoresWithinXMiles(double latitude, double longitude, double miles) { System.Diagnostics.Debugger.Launch(); var userCoords = new GeoCoordinate(latitude, longitude); return this.Where(s => (s.LatLong.GetDistanceTo(userCoords) / METERS_PER_MILE) <= miles); } }

2. Build and deploy your code

3. Start the process

4. When it hits the Debugger.Launch() line, it’ll prompt you to choose how you want to open the debugger instance:

  • Choose the appropriate version of Visual Studio and click OK

Now Visual Studio will open and you can debug the code.

Leave a Comment