2

i'm trying to open a process with c# and react to it, when it's been closed. This work's for me:

private void StartProc()
    {
        var process = new System.Diagnostics.Process { StartInfo = { FileName = "PathTo.exe" } };
        process.Start();
        process.EnableRaisingEvents = true;
        process.Exited += this.Editor_Exited;
    }

    private void Editor_Exited(object sender, EventArgs e)
    {
        MessageBox.Show("Process canceled");
    }

Lets say I'm opening a text editor with this code. If there is already an instance of this text editor the code won't open a second instance and also jumps instant in the Editor_Exited Code.

I want the code to open a new instance and don't jump in the Editor_Exited code.

  • Which text editor are we talking about? Many editors close themself if an instance already exists. – Hyarus Nov 07 '18 at 11:39
  • Looks like the behavior of another application is to close immediately if it is already opened and this is not on your control. This is not always true, for example, you can open any number of `notepad.exe` application. – vasily.sib Nov 07 '18 at 11:40
  • Exactly. I would expect that it's configured to allow just a single instance so when you start it, it checks if it's already running and if it's, it will just exit. Check if your text editor allows you to control it. – David Ferenczy Rogožan Nov 07 '18 at 11:41
  • It's Visual Studio Code. But why does it trigger the event? I mean its the "old" instance which closes and not the one I opened, right? Maybe I'm wrong and VS just doesn't allow to open the second instance, so would there be any solution in your mind? And also I thought VS Code would allow multiple instances. –  Nov 07 '18 at 11:42
  • @DanielRose It is the Text Editor which does not allow multiple instances. – Hyarus Nov 07 '18 at 11:52
  • @Hyarus You are right but I'm able to open multiple VS Code windows manually. How can i acces this? –  Nov 07 '18 at 11:53
  • @DanielRose I think i missunderstood here. So is VS Code the editor you want to open with your code? I thought you were talking about your IDE. – Hyarus Nov 07 '18 at 11:56
  • If so, did you try using the -n [argument](https://code.visualstudio.com/docs/editor/command-line#_core-cli-options)? – Hyarus Nov 07 '18 at 12:04
  • So I'm trying to open Visual Studio Code with a new window and in this window I want to display some code. I will try the "-n argument". –  Nov 07 '18 at 15:09
  • @Hyarusit works and opens a new instance but it fires the `Editor_Exited` event, do you know why this happens? –  Nov 08 '18 at 07:51

1 Answers1

0
string processName = "PathTo.exe";

var process = new System.Diagnostics.Process { StartInfo = { FileName = processName  } };
if (process.Start())
{
  process.EnableRaisingEvents = true;
  process.Exited += this.Editor_Exited;
}
else
{
 var p = Process.GetProcessesByName(processName);
 p.WaitForExit();
}

I get this is not 100% what you are asking for, but its a work around

Lord Darth Vader
  • 1,895
  • 1
  • 17
  • 26