Bali C
Bali C

Reputation: 31231

C# Execution Loop

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.

Upvotes: 2

Views: 3150

Answers (3)

Andrey
Andrey

Reputation: 1601

Try wait for process to start. It cannot start immidiatly even in theory.

Second. You check if process is running is incorrect, resulting in ~100 calls to RDP()

using System;
using System.Diagnostics;
using System.Threading;

class Watchdog 
{
    static void Main(string[] args)
    {
        while(true) {
            if (!IsRdpRunning())
                RunRdp();
            Thread.Sleep(1000);
        }
    }

    private static void RunRdp()
    {
        Process rdp = new Process();
        rdp.StartInfo = new ProcessStartInfo(@"C:\Alistair\Default.rdp");
        rdp.Start();
        Thread.Sleep(10000);
    }

    private static bool IsRdpRunning()
    {
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.Contains("mstsc"))
                {
                    return true;
                }
            }

            return false;
    }
}

Upvotes: 0

Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10967

static void Main(string[] args)
{
    RDP();
    while(true)
    {
        if(Process.GetProcessesByName("mstsc").Length == 0)
            RDP();
        Thread.sleep(300); // Use any value which is confortable with you're request
    }
}

private static void RDP()
{
    Process rdp = new Process();
    rdp.StartInfo = new ProcessStartInfo("C:\\Alistair\\Default.rdp");
    rdp.Start();
}

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.HasExited if true than restart.

Upvotes: 3

Valamas
Valamas

Reputation: 24729

I wonder if Process.GetProcesses(), since it is a method, gets a fresh list of processes. Maybe try storing the result in a list first.

Upvotes: 0

Related Questions