C# 执行循环
我刚刚用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是代码优化的一部分,它不会让你的CPU 100% 挂起。
您可以做的是检查 Process 事件 onExit(不确定),或者仅检查 rdp.HasExited 是否为 true 而不是重新启动。
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.我想知道 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.
尝试等待进程启动。即使在理论上,它也不能立即启动。
第二。您检查进程运行是否不正确,导致约 100 次调用 RDP()
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()