Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#

Automate the "attach to process"

Rate me:
Please Sign up or sign in to vote.
4.86/5 (18 votes)
25 Oct 2013CPOL1 min read 55.7K   871   34   17
Automating the Attach to Process from within Visual Studio.

Introduction

Often you can catch yourself doing repetitive work. Sometimes I even manage to get annoyed before I realize what the problem is. This article is about my latest idea: automating the "attach to process" from Visual Studio.

Background

Developing a client/server solution will often bring you in a situation where you start debugging the client and then realize that there is a problem with the server, so you start the client (again) and attach Visual Studio to the server in order to debug the problem. After a while, this routine becomes really annoying. While reading through some of the Microsoft pages regarding EnvDTE, I had an idea. The idea is basically to get hold of the running instance of Visual Studio from the client and use EvnDTE to attach to the server.

Using the Code

I created an extension method to extend the System.Diagnostics.Process. This seemed rational since I had to use a Process to start the server from the client.

The code is as follows:

C#
public static void Attach(this System.Diagnostics.Process process)
{
  // Reference Visual Studio core
  DTE dte;
  try
  {
    dte = (DTE) Marshal.GetActiveObject(progId); 
  } catch (COMException) {
    Debug.WriteLine(String.Format(@"Visual studio not found."));
    return;
  }
  
  // Try loop - Visual Studio may not respond the first time.
  int tryCount = 5;
  while (tryCount-- > 0)
  {
    try
    {
      Processes processes = dte.Debugger.LocalProcesses;
      foreach (Process proc in processes.Cast<Process>().Where(
        proc => proc.Name.IndexOf(process.ProcessName) != -1))
      {
        proc.Attach();
        Debug.WriteLine(String.Format
		("Attached to process {0} successfully.", process.ProcessName));
        break;
      }
      break;
    }
    catch (COMException)
    {
      System.Threading.Thread.Sleep(1000);
    }
  }
}

Using the extension method can be done like this (example):

C#
Process p = new Process();
FileInfo path = new FileInfo(Assembly.GetExecutingAssembly().Location);
string pathToProcessToDebug = Path.Combine
    (path.Directory.FullName, @"..\..\..\ProcessToDebug\Bin\Debug\ProcessToDebug.exe");
ProcessStartInfo psi = new ProcessStartInfo(pathToProcessToDebug);
p.StartInfo = psi;
p.Start()
p.Attach(); // <-- Extension method 

Points of Interest

The idea could be implemented in any number of ways, but I was keen on using an extension method. I looked at the System.Diagnistics.Debug class for an appropriate place to add the method - but static classes cannot be extended.

History

  • Version 1.0 (First version).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Denmark Denmark
Name: Niel Morgan Thomas
Born: 1970 in Denmark
Education:
Dataengineer from Odense Technical University.
More than 20 years in IT-business.
Current employment:
Cloud architect at University College Lillebaelt

Comments and Discussions

 
GeneralMy vote of 1 Pin
axdowney9-Feb-22 19:19
axdowney9-Feb-22 19:19 
GeneralRe: My vote of 1 Pin
Niel M.Thomas9-Feb-22 19:36
professionalNiel M.Thomas9-Feb-22 19:36 
PraiseMy vote of 5 Pin
Gerry198331-Jan-17 2:29
Gerry198331-Jan-17 2:29 
Questionwhat is progId Pin
vikas.yerram22-Nov-14 19:32
professionalvikas.yerram22-Nov-14 19:32 
AnswerRe: what is progId Pin
Niel M.Thomas22-Nov-14 21:23
professionalNiel M.Thomas22-Nov-14 21:23 
GeneralRe: what is progId Pin
vikas.yerram23-Nov-14 15:59
professionalvikas.yerram23-Nov-14 15:59 
QuestionCould any one tell me the simplest step to use it? Pin
leiyangge16-Jan-14 4:30
leiyangge16-Jan-14 4:30 
AnswerRe: Could any one tell me the simplest step to use it? Pin
Niel M.Thomas16-Jan-14 4:35
professionalNiel M.Thomas16-Jan-14 4:35 
GeneralRe: Could any one tell me the simplest step to use it? Pin
leiyangge16-Jan-14 14:32
leiyangge16-Jan-14 14:32 
QuestionThis may help too Pin
Mauro Leggieri5-Oct-11 15:19
Mauro Leggieri5-Oct-11 15:19 
GeneralMy vote of 5 Pin
ShdNx4-Oct-11 5:03
ShdNx4-Oct-11 5:03 
GeneralMy vote of 5 Pin
GPUToaster™3-Oct-11 23:22
GPUToaster™3-Oct-11 23:22 
GeneralMy vote of 5 Pin
HoyaSaxa9330-Sep-11 10:43
HoyaSaxa9330-Sep-11 10:43 
GeneralRe: My vote of 5 Pin
kiran dangar4-Oct-11 0:36
kiran dangar4-Oct-11 0:36 
Nice one
---------
Cheers,
Kiran (http://kirandangar.wordpress.com/)

QuestionImpeccable timing guys Pin
rocware2129-Sep-11 13:25
rocware2129-Sep-11 13:25 
Suggestionseems a bit excessive to me PinPopular
Blkbam29-Sep-11 7:19
Blkbam29-Sep-11 7:19 
GeneralRe: seems a bit excessive to me Pin
Niel M.Thomas29-Sep-11 8:08
professionalNiel M.Thomas29-Sep-11 8:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.