When 'F5' debugging and attaching to processes is slow

4 min read

Learn how you can speed up your debugging process in Visual Studio for SharePoint projects. Create a shortcut to launch & attach the debugger.


Nothing new in this post… just seems ever few weeks/months you talk to someone that wasn’t aware you could do this. The last person I spoke to had a hard time finding this so I’m posting it for their, and now your, edification.

The Problem

MCMS 2002 and WSS v2 Web Part developers know that you can’t just hit F5 (see footer) to build and enter into your debugging environment without any extra configuration in Visual Studio. In order to debug your process, you do the following:

  • Build your solution
  • If it’s not already started, fire off one request to your solution/project/whatever to get the (in the case of IIS 5.x) ASP.NET worker process to start up or (in the case of IIS 6.0, w3wp.exe)
  • Using Attach the Visual Studio debugger to the process
  • Start debugging
Visual Studio Dialog

Visual Studio Dialog

Visual Studio Dialog

Visual Studio Dialog

That takes no fewer than 4 mouse clicks (VS Debug Menu > attach to process… > select aspnet_wp.exe/w3wp.exe > click OK). I’m not much of a mouse guy… much prefer shortcuts with my keyboard. The less I have to take my hands off the keyboard while working on a project, the more productive I can be. Not to mention, you could do this many times in the course of working on a project… say you jump into the debugger once every three minutes. On my relatively new laptop, that takes about 6 seconds. Over the course of a day, that adds up (for those few souls who actually work 8hrs a day, that’s just under 3 minutes a day). OK, so it’s not THAT much of an impact… but it’s a repetitive process… can’t that be automated? How do you get around this annoying use of the mouse?

The Solution

Set up a macro to automatically attach to the desired process, set a shortcut to trigger that macro, and another shortcut to detach from the process. The following assumes you’re working in Visual Studio 2005. The steps are similar if not identical in VS 2003. VS 2005 just has sexier screenshots (ok, just better looking). :P

First, we’ll create the macro:

  • Right-click MyMacros in the Macro Explorer tool window (bring up the Macro Explorer by pressing ALT+F8) and select New Module….
  • Give your module a name, I used VSDebugger.
  • Right-click your new module, in my case VSDebugger, and select New macro.
  • Change the name of the macro to something that makes more sense; I used AttachTo_ASPNETWP.
  • Replace the default macro added to your module with the following code. I’ve heavily commented it so you can understand what’s going on:
 1' This routine attaches to the ASP.NET worker
 2processSub AttachTo_ASPNETWP()
 3  Dim attached AsBoolean = False
 4  Dim proc As EnvDTE.Process
 5  Dim processToAttachTo AsString
 6
 7  ' name of the process to attach to
 8  processToAttachTo = "aspnet_wp.exe"
 9
10  ' iterate through all processes running on the local machine
11  ForEach proc In DTE.Debugger.LocalProcesses
12    ' if the last [X] characters of the process name = name of the process...
13    If (Right(proc.Name, Len(processToAttachTo)) = processToAttachTo) Then
14      ' attach to the process
15      proc.Attach()
16      ' set a flag that we've attached to the process & exit the for loop
17      attached = True
18      ExitFor
19    EndIf Next
20  EndFor
21
22  ' if macro didn't find process running, notify user
23  If attached = False Then
24    MsgBox(processToAttachTo & " is not running")
25  EndIf
26EndSub

Next, we need to create a keyboard shortcut:

  • In Visual Studio, open the Options dialog (Tools | Options).
  • Select the Keyboard node under the Environment category.
  • In the Show commands containing: textbox, enter VSDebugger (or the name you chose for your module) to find the command in the listbox.
    • To digress for a second… why in the world did the VS IDE developers (1) give us 10,000 items in such a small listbox (only see 5 items at a time) and/or (2) not make the Options dialog resizable? Man, that seems so obvious to fix.
  • Place the cursor in the Press Shortcut keys: input box and type the key combination you’d like to use to fire your macro off. As you can see in the screenshot, I use a two step command. I press CTRL+SHIFT+P, release, and then press A. Why? The first part is easy to remember as it’s binding to a process P. The second command is Attaching to the process:
Visual Studio Dialog

Visual Studio Dialog

Repeat the same process to create a shortcut to detach from the process. In this case, search for detachall… I used the key combination CTRL+SHIFT+P, D (so A attaches, D detaches).

Now you’re good to go! After building your solution CRTL+SHIFT+B), hit your key combination to attach to the process.

Happy debugging!

Share this article

Feedback & comments