简体   繁体   English

通过无限while循环防止高CPU使用率的正确方法(控制台/窗体混合)

[英]Correct way to prevent high CPU usage with infinite while loop (Console/Forms hybrid)

I put together a console/forms hybrid program, and to achieve the behavior I was looking for I had to "run" the console in a thread, and then launch the dashboard with another. 我整理了一个控制台/窗体混合程序,并实现了我想要的行为,我必须在一个线程中“运行”控制台,然后与另一个控制台一起启动仪表板。

I used BackgroundWorker for this. 我为此使用了BackgroundWorker Essentially in the Main method I start the console background worker which in its DoWork block performs a normal while loop to keep a console going. 本质上,在Main方法中,我启动了控制台后台工作程序,该后台工作程序在其DoWork块中执行正常的while循环以保持控制台正常运行。 Repetitively checking for input from the console and passing it along to it corresponding action. 重复检查来自控制台的输入,并将其传递给相应的操作。

while (true && !Program.Shutdown)
{
    String consoleInput = "";

    consoleInput = Program.ReadFromConsole();

    if (String.IsNullOrWhiteSpace(consoleInput))
        continue;

    try
    {
        Program.Shell(consoleInput);
    }
    catch (Exception ex)
    {
        Program.WriteToConsole(ex.Message);
    }
}

After the background worker for starting the console is called ie ConsoleBackgroundWorker.RunWorkerAsync() I call a method which performs a loading sequence. 在启动控制台的后台工作程序被调用之后,即ConsoleBackgroundWorker.RunWorkerAsync()我调用了执行加载序列的方法。

This loading sequence CAN call the second background worker which internally launches the dashboard on a separate thread. 此加载序列可以调用第二个后台工作程序,该后台工作程序在单独的线程上内部启动仪表板。

Now the problem I faced was the loop at the end of the load sequence. 现在我面临的问题是加载序列末尾的循环。 This loop prevents everything from shutting down until both of the background workers have completed (the forms closed or the console sent a command to shutdown) 此循环防止一切都关闭,直到两个后台工作人员都完成为止(窗体关闭或控制台发送了关闭命令)

while (ConsoleBackgroundWorker != null && DashboardBackgroundWorker != null)
{
    /* I'm not sure of how else to fix this. But 
     * this corrects the high CPU usage reported 
     * in the Task Manager. */
    System.Threading.Thread.Sleep(10);
}

The problem was CPU usage, it was constantly sitting at 30% and this is without the line you see above System.Threading.Thread.Sleep(10) 问题是CPU使用率,它一直保持在30%,这与System.Threading.Thread.Sleep(10)上方的行完全不同

My question is, is this ok? 我的问题是,可以吗? and if not what solution do I have? 如果没有,我有什么解决方案? I noticed this did correct the issue, the CPU now rides at 0% when the application is not doing anything. 我注意到这确实解决了问题,当应用程序不执行任何操作时,CPU现在的运行速度为0%。 I also haven't seen any performance changes either. 我也没有看到任何性能变化。 But it has not really been "pushed" yet so I couldn't say for sure on the latter observation. 但这还没有真正“推开”,因此我不能肯定地说后面的观察。

Instead of polling (checking every 10ms, as you've done) the "better" (or at least less CPU intensive) solution would be to use some kind of wait-functions. 与其轮询(每10毫秒检查一次),不如“更好”(或至少减少CPU占用)的解决方案是使用某种等待功能。 You could use a ManualResetEvent maybe, or any of the other synchronization objects available in the System.Threading namespace. 您可以使用ManualResetEvent ,也可以使用System.Threading命名空间中可用的任何其他同步对象。 Depends on what you need. 取决于您的需求。 Then you can just call .Wait() on that object and it will block the thread (0% actual CPU usage) until some kind of signal arrives from another thread. 然后,您可以在该对象上调用.Wait() ,它将阻塞线程(实际CPU使用率为0%),直到某种信号从另一个线程到达为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM