I created a management application that also allows you to quickly access a remote desktop session for remote computers. I need to wait until the process ends, so I can close the VPN connection to the remote server. Everything is working fine, except waiting for the process to complete.
The following code is used to start the MSTSC process and wait for it to complete:
var process = new Process { StartInfo = new ProcessStartInfo("mstsc.exe"), EnableRaisingEvents = true }; process.Exited += (o, e) => Console.WriteLine("Process stopped."); process.Start(); Console.ReadLine();
The Exited event occurs almost immediately after the program starts. When I replace mstsc.exe with notepad.exe , everything works as expected. I thought that MSTSC could unlock itself and interrupt the initial process.
But you can wait until the end of MSTSC using the following command (from the command line):
start /wait mstsc.exe
This command does not return until I log out of the Remote Desktop session. Given this information, I replaced the code as follows:
var process = new Process { StartInfo = new ProcessStartInfo("cmd.exe"), Arguments = "/c start /wait mstsc.exe", EnableRaisingEvents = true }; process.Exited += (o, e) => Console.WriteLine("Process stopped."); process.Start(); Console.ReadLine();
This will start CMD.exe and it will issue the start /wait mstsc.exe . If this ends, the CMD process ends, and I'm fine (with an unpleasant workaround, but it's fine). Unfortunately, this does not happen. The CMD process stops immediately. Does anyone know what I'm doing wrong?
source share