C# 执行循环

发布于 2024-12-03 07:37:23 字数 797 浏览 0 评论 0原文

我刚刚用 C# 创建了一个小程序,没什么花哨的,可以打开 rdp 文件。然后它进入无限循环并不断检查 mstsc 是否正在运行,如果是,则不执行任何操作,如果不是(用户已关闭会话),则重新打开。我运行了下面的代码,它严重影响了我的 CPU,然后在蓝屏后不久,minidump 说这是因为“这表明在执行从非特权代码转换为特权代码的例程时发生了异常。”

不确定这意味着什么,但是您知道这段代码有什么问题吗?

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

更新:我认为无限循环所需的处理能力可能是罪魁祸首,但我尝试循环 5 次但结果相同。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各自安好 2024-12-10 07:37:23
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();
}

这是代码优化的一部分,它不会让你的CPU 100% 挂起。

您可以做的是检查 Process 事件 onExit(不确定),或者仅检查 rdp.HasExited 是否为 true 而不是重新启动。

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.

爱冒险 2024-12-10 07:37:23

我想知道 Process.GetProcesses() 因为它是一种方法,是否会获取新的进程列表。也许尝试先将结果存储在列表中。

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.

优雅的叶子 2024-12-10 07:37:23

尝试等待进程启动。即使在理论上,它也不能立即启动。

第二。您检查进程运行是否不正确,导致约 100 次调用 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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文