Relaunch program in VS 2010 debugger

105 Views Asked by At

I have an application that occasionally requires you to restart it after changing preferences. I do this by calling:

System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);

That's great for the released version, but it's really annoying when I'm debugging and it relaunches the program outside the debugger whenever I need to change the preferences.

After some research, I've tried attaching to the debugger from code, but the very code I'm trying to run is running in the debugger already, and the application will be killed shortly. So I wrote an external program that can be called after relaunching the application (and freeing up the debugger) that supposedly attaches it to the debugger. Unfortunately this doesn't really do the job either. It appears to get attached to the debugger, but it doesn't actually let me do any debugging. It just craps out with an error if I try to pause execution.

Any ideas?

2

There are 2 best solutions below

2
On BEST ANSWER

After more research, I realized all I wanted to do was programmatically hit the Restart button. I found this:

EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
dte.ExecuteCommand("Debug.Restart");

Works great!

0
On

You may not be able to debug with your code if you are using a Process object and its Attach method from the EnvDTE namespace rather than the Process2 object and its Attach2 method from the EnvDTE80 namespace. The following snippet should work:

foreach (Process2 process in Dte.Debugger.LocalProcesses) {
   if (process.ProcessID == processId) {
       process.Attach2();
       Dte.Debugger.CurrentProcess = process;
   }
}

May also be of interest here: the Visual Studio team has released a Visual Studio extension that allows automatically attaching child processes to the current debugger: Introducing the Child Process Debugging Power Tool.

It is available on the Gallery for Visual Studio 2013 and above.