I have just created a small program, nothing fancy, in C# that opens an rdp file. It then goes into an infinite loop and keeps checking if mstsc is running, if it is then it does nothing, if it isn’t (user has closed the session), it re – opens. I ran the code below and it hammered my CPU and then shortly after blue screened, minidump says it was because “This indicates that an exception happened while executing a routine that transitions from non-privileged code to privileged code.”
Not sure what this means, but any ideas what is wrong with this code?
static void Main(string[] args)
{
RDP();
for (int i = 1; i > 0; i++)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains("mstsc.exe"))
{
}
else
{
RDP();
}
}
}
}
private static void RDP()
{
Process rdp = new Process();
rdp.StartInfo = new ProcessStartInfo("C:\\Alistair\\Default.rdp");
rdp.Start();
}
Update: I thought that the processing power needed for the infinite loop might have been to blame but I tried looping for 5 times but same result.
Here is a portion of code optimised, and it will not hang your CPU at 100 %.
What you can do is to check the Process event onExit(not sure), or just check
rdp.HasExitedif true than restart.