How to wait until MSTSC.exe exits

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?

Answers


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();

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.


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.


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.


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;
}

Need Your Help

iTextSharp: Display “Open/Save” dialog box when PDF file is created

c# pdf itextsharp save-as

I am using iTextSharp to create a PDF file (I use C#). The file is created fine; however, it is saved in a folder. The file is created in response to a link button click and I want to display the

Updating Select All/Deselect All Checkbox

javascript jquery html forms checkbox

The following code works fine when selecting all checkboxes, however it uses the the input type of button. How could i change this into an anchor (eg. like a link) instead of an input type of but...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.