How to wait until MSTSC.exe exits

4.3k views Asked by At

I have created a management application that also allows to quickly access a remote desktop session to remote machines. I need to wait until the process ends, so I can close the VPN connection to the remote server. Everything works fine, except waiting for the process to end.

The following code is being used to start the MSTSC process and wait until it ends:

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 is raised almost immediately after the program starts. When I replace mstsc.exe with notepad.exe everything works as expected. I thought that MSTSC might fork itself and abort the initial process.

But it is possible to wait for MSTSC to end using the following command (from the commandline):

start /wait mstsc.exe

This command doesn't return until I exit the remote desktop session. Given that information I replaced my code with this:

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 would run CMD.exe and it will issue the start /wait mstsc.exe command. If that ends, the CMD process ends as well and I'm fine (with a nasty workaround, but okay). Unfortunately, this doesn't happen. The CMD process terminates immediately. Somebody knows what I am doing wrong?

6

There are 6 answers

0
mswietlicki On BEST ANSWER
process.WaitForExit();

Won't work because mstsc on start opens new copy of itself and closes original.

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

Will work but it's awful workaround.

Update 1:

It seems that mstsc closes original process but NOT it's output stream! So you can wait for process StandardOutput to close.

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 don't want to block 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();
0
Rubino Nóbrega On

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
chris.house.00 On

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

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

This overload causes the current thread to wait indefinitely to wait until the process exits. There's also an overload that allows you to specify the number of milliseconds you'd like to wait.

0
Ramon de Klein On

There are multiple MSTSC processes running, so it's difficult to wait for one. What I don't understand is that CMD.EXE can do it when I use the start /wait command.

2
user277768 On

Here is the link at MSDN about starting mstsc,

It might be answer to your problem with mstsc closing immediately after running (raising Exited event). Try changing in Visual Studio target platform to AnyCPU.

Let's say your machine is 64bit Windows, your app is 32bit. The app runs 32bit mstsc. 32bit mstsc detects that Windows is 64bit, tries to close itself and run 64bit mstsc (Exited event is raised at that moment even though mstsc starts GUI window). Changing target platform solved my issue.

0
lison On

You cannot wait for mstsc.exe process. Say exactly, you cannot simply wait for end of remote desktop. When I observed mstsc.exe process by Process Monitor, mstsc passed his work to svchost, mstsc.exe ended, but remote desktop was still run.

But I wrote script for remoting application. Script remoteCmd.cmd starts remoteApplication, remote machine creates a temp file ( \\tsclient\c..\temp\xxx) and remoteCmd.cmd waits until temp file exists. See https://github.com/turzik/WindowsScripts/tree/master/remoteApp