1

In order to start another instance of my program I did something like:

private void button1_Click(object sender, EventArgs e)
{
    Process p = new Process();
    p.StartInfo.FileName = Application.ExecutablePath;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    p.Start();
}

And found that stopping the debugger didn't stop the new window, only the first (-launching) window.

How do I programmatically get the new process to be "under" VS?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • 1
    @antisanity: It's not an exact duplicate as ispiro wants the second instance to be a child of the existing process (one that is killed when the main process is torn down). – user7116 Dec 14 '11 at 15:41

5 Answers5

2

You can Change the Start Action for Application Debugging

  1. Right click on your project
  2. Properties
  3. Debug
  4. Start external program

And set the program you want to launch.

If you want to attach to an another instance programmatically, a duplicate question can be found here:

Wich refer this article:

Community
  • 1
  • 1
Djoul6
  • 309
  • 1
  • 7
  • Thanks. I tried copying the code from the supplied link into my code, I added a `using EnvDTE;`, a reference to envdte (from .net), and changed the static DTE to an instance (VS complained about it), but then I got an error: _Retrieving the COM class factory for component with CLSID...failed due to the following error: 80040154 Class not registered._ What am I doing wrong? Or can it be because I'm using VS _express_? – ispiro Dec 14 '11 at 16:43
2

That Process that you get back has a handle to the running process. You could keep a hold of that in a member variable, rather than a local variable, and on form closing, kill the process.

http://msdn.microsoft.com/en-us/library/e8zac0ca.aspx

tbddeveloper
  • 2,407
  • 1
  • 23
  • 39
1

Since you're starting your own program a second time, you know it is a GUI. You can keep the Process reference around and call CloseMainWindow (or Kill) on each of them in your FormClosing event handler:

private List<Process> children = new List<Process>();

private void button1_Click(object sender, EventArgs e)
{
    Process p = new Process();
    p.StartInfo.FileName = Application.ExecutablePath;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    p.Start();

    children.Add(p);
}

private Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    foreach (Process p in this.children)
    {
        // posts WM_CLOSE to the main handle of the process
        // which allows a graceful exit, as if the user clicked [X]
        p.CloseMainWindow();
        // p.Kill(); // less graceful, just kill
    }
}
user7116
  • 63,008
  • 17
  • 141
  • 172
0

Debug -> Attach to Process and select your process from the list.

Eugene
  • 1,515
  • 1
  • 13
  • 23
0

If by "under VS" you mean having Visual Studio be able to debug the external process you may want to consider the "Attach to Process" strategy.

http://msdn.microsoft.com/en-us/library/c6wf8e4z.aspx

syneptody
  • 1,250
  • 1
  • 10
  • 27