How to wait for MSTSC.exe to exit

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?

+4
source share
5 answers
 process.WaitForExit(); 

It will not work, because mstsc at startup opens a new copy of itself and closes the original.

 process.WaitForExit(); process = Process.GetProcessesByName(process.ProcessName).First(); process.WaitForExit(); 

It will work, but it is a terrible workaround.

Update 1:

It seems that mstsc closes the original process, but does not output the thread! This way, you can wait for the StandardOutput process to complete.

 var process = new Process { StartInfo = new ProcessStartInfo("mstsc.exe") { UseShellExecute = false, RedirectStandardOutput = true } }; process.Start(); process.StandardOutput.ReadToEnd(); //This will wait for stream to close. 

Or, if you do not want to block the current thread:

 var process = new Process { StartInfo = new ProcessStartInfo("mstsc.exe") { UseShellExecute = false, RedirectStandardOutput = true } }; process.Start(); var outputResultPromise = process.StandardOutput.ReadToEndAsync(); outputResultPromise.ContinueWith(o=> Console.WriteLine("Stream closed")); Console.ReadLine(); 
+3
source

Here is the MSDN link for running mstsc ,

This may be the answer to your problem with closing mstsc immediately after launch (raising the Exited event). Try changing in the target Visual Studio environment on AnyCPU .

Let's say your machine is 64-bit Windows, your application is 32-bit. Application launches 32-bit mstsc. 32bit mstsc detects that Windows is 64 bit, tries to shut itself down and run 64-bit mstsc (the Exited event occurs at that moment, although mstsc launches the GUI window). Changing the target platform solved my problem .

+1
source

You need to call WaitForExit () after calling Start ():

 process.Start(); process.WaitForExit(); 

This overload causes the current thread to wait indefinitely to wait for the process to complete. There is also an overload that allows you to specify the number of milliseconds you want to wait.

0
source

There are several MSTSC processes, so it is difficult to wait. I do not understand that CMD.EXE can do this when I use the start /wait command.

0
source

this worked with me:

 process.Start(); Thread.Sleep(2000); while(getNumProcesses() > 0) process.WaitForExit(); private static int getNumProcesses() { Process[] myProcesses = Process.GetProcessesByName("mstsc"); return myProcesses.Length; } 
0
source

Source: https://habr.com/ru/post/1498624/


All Articles