0

The Problem I am facing as my title suggests is determining if a program that I started using command lines has closed or ended.

I have tried many different ways in which to check if the program has exited, using process ID even using a loop, unfortunately I can’t seem to get it to work. I believe that it is due to the fact that I created a process which starts program using command lines rather than a method pointing to a directory to start the program. The reason I used command lines is that the path to the program is located in Program (x86), which wasn’t working for me due to the space I guess.

The part of the code that starts the application

ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c cd / & /x & cd PROGRA~2 & cd Truvelo & cd DCM & start dcm.exe");
int exitCode;
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
exitCode = proc.ExitCode;
}

I did try something similar like this however I believe it didn’t work as I started the application using another process using command lines.

System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName(sProcessName);
  if (proc.Length > 0)
  {
      MessageBox.Show(String.Format("{0}is  running!", sProcessName), sProcessName);
  }
  else
  {
      MessageBox.Show(String.Format("{0}is not running!", sProcessName), sProcessName);

   }

I am hoping someone can help me in a logical way to determine if I closed the application or for the code I am writing to know when the application is started and then tell me as soon as the application is closed

Thank you

Vijay Yadav
  • 91
  • 13
  • Somehow related: https://stackoverflow.com/questions/194157/c-sharp-how-to-get-program-files-x86-on-windows-64-bit – nilsK May 08 '19 at 08:49
  • `var x86path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);`. About the Process termination, see this [sample code](https://stackoverflow.com/a/51682585/7444103) (using the Process' `Exited` event). – Jimi May 08 '19 at 08:57
  • Hi i have tried something similar to that however my program just doesnt seem to start, it is as if it starts but then suddenly closes itself `ProcessStartInfo start = new ProcessStartInfo(x86path + "/Truvelo/DCM/dcm.exe");` Is that correct – Vijay Yadav May 08 '19 at 09:06
  • Use [Path.Combine](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine) to combine parts of a path. Don't declare the Process with a `using` statement. You'll dispose of it. Dispose of the Process in the `Exited` event (as shown in the sample code). – Jimi May 08 '19 at 09:21
  • You can just have `proc.StartInfo.FileName = Path.Combine(x86path, @"Truvelo\DCM\dcm.exe");`. Subscribe to the `Exited` event: `proc.EnableRaisingEvents = true; proc.Exited += (obj, evt) => { Console.WriteLine("Closed"); proc?.Dispose(); };`. Then, start the process: `proc.Start();`. That's all. All in the sample code I linked. – Jimi May 08 '19 at 09:30
  • Hi I have tried that however every time i do as soon as the program window opens the program has been closed event tirggers straight away – Vijay Yadav May 08 '19 at 09:48

1 Answers1

0

You are using cmd start and this command is not waiting for started application to end. Your proc variable is tracking process started so cmd not dcm and that one is ending when dcm is started not ended.

You can either add /WAIT command option or not use start command and just executed dcm.exe directly.

/c cd / & /x & cd PROGRA~2 & cd Truvelo & cd DCM & start dcm.exe /WAIT

/c cd / & /x & cd PROGRA~2 & cd Truvelo & cd DCM & dcm.exe

or not use cmd at all and just start dcm from c# even more directly:

new ProcessStartInfo(@"C:\Program Files (x86)\Truvelo\DCM\dcm.exe")
Rafal
  • 12,391
  • 32
  • 54