I have a WPF application that calls mstsc.exe for remote desktop connections. It basically calls it with a parameter (path to rdp file). This code works fine when the user lets the code execute until the end. However, mstsc.exe temporarily displays a popup saying "Connecting to..." while it is connecting, and this popup has a Cancel button. The problem is that when the user clicks the Cancel button, the UI blocks. It seems to be stuck in my while loop because I'm waiting for the RDP window to open. How can I handle this situation in my code if the user cancels?
private int LaunchMstscProcess(string rdpFullPath)
{
//Launches the underlying rdp file.
m_proc = new Process { StartInfo = { FileName = "mstsc.exe", Arguments = rdpFullPath } };
m_proc.Start();
//Wait until process is started and main rdp window is created.
while (m_proc.MainWindowHandle == IntPtr.Zero)
{
Thread.Sleep(m_rdpDelay);
}
return m_proc.Id;
}