一个应用程序在系统上消耗几乎100%的CPU

本文关键字:100% CPU 系统 一个 应用程序 | 更新日期: 2023-09-27 18:29:54

我的应用程序使用一些Web服务,连接到服务器并位于系统托盘上。应用程序只是从用于连接到服务器的第三部分应用程序(本地)中读取。如果它读取到任何不适当的行,则触发/处理适当的事件。太好了。已经在我们的正常工作系统上测试过了,它运行得很好。在我的系统Quad 2.66GHz和3GB的RAM上,应用程序消耗25的CPU。

最近在一个包含2GB RAM系统的PIV 3.0Ghz系统上进行了检查,该应用程序消耗了98个CPU。

这可能是什么?在一个更好的处理器上,该应用程序消耗的CPU几乎是我的4倍。为什么会这样?同样的原因可能是什么。任何想法都可以解决。

一旦连接,这就是激活的代码。运行cmd以使用第三方应用程序,我使用此代码读取相同的输出:

    private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        string d = e.Data;
        if (!string.IsNullOrEmpty(d))
        {
            if (sb != null)
                sb.Append(d + "'n");
            //Console.WriteLine("LINE = " + d);
            ERROR_CODE = -1;
            if (d.IndexOf("Initialization Sequence Completed") >= 0)
            {
                connected = true;
                Console.WriteLine("********* Connected = " + connected);
                return;
            } else if (isInValidLine(d))
            {
                if (d.IndexOf("Exiting") >= 0 || d.IndexOf("The requested name is valid but does not have an IP address.") >= 0)
                {
                    if (caughtExp == false)
                    {
                        connected = false;
                        caughtExp = true;
                        errorMsg = d;
                        ERROR_CODE = ERROR_EXIT;
                        OnOpenVpnDisConnectionEvent();
                    }
                }
                else if (d.IndexOf("errno=", StringComparison.OrdinalIgnoreCase) > 0
                    || d.IndexOf("error:2006D080", StringComparison.OrdinalIgnoreCase) >= 0
                    || d.IndexOf("code=995", StringComparison.OrdinalIgnoreCase) >= 0
                    || d.IndexOf("There are no TAP-Win32 adapters on this system", StringComparison.OrdinalIgnoreCase) >= 0
                    || d.IndexOf("Error opening configuration file", StringComparison.OrdinalIgnoreCase) >= 0
                    || (d.IndexOf("CreateIpForwardEntry: Access is denied.", StringComparison.OrdinalIgnoreCase) >= 0 
                                        && d.IndexOf("[status=5 if_index=", StringComparison.OrdinalIgnoreCase) >= 0)
                    || d.IndexOf("CreateFile failed on TAP device") >= 0  )
                {
                    // Want to handle all and go for ReConnect, atmost possible
                    caughtExp = true;
                    connected = false;
                    errorMsg = d;
                    ERROR_CODE = ERROR_FATAL;
                    OnOpenVpnDisConnectionEvent();
                }
                return;
            }
        }
        return;
    }
    private bool isInValidLine(string line)
    {
        if (line.IndexOf("errno=", StringComparison.OrdinalIgnoreCase) >= 0 
            || line.IndexOf("error:2006D080", StringComparison.OrdinalIgnoreCase) >= 0
            || line.IndexOf("code=995", StringComparison.OrdinalIgnoreCase) >= 0
            || line.IndexOf("There are no TAP-Win32 adapters on this system", StringComparison.OrdinalIgnoreCase) >= 0
            || line.IndexOf("Error opening configuration file", StringComparison.OrdinalIgnoreCase) >= 0
            || line.IndexOf("Exiting") >= 0
            || line.IndexOf("The requested name is valid but does not have an IP address.") >= 0
            || line.IndexOf("CreateFile failed on TAP device") >= 0    // && (line.IndexOf("General failure (ERROR_GEN_FAILURE) (errno=31)") >= 0) ) 
            )
        {
            return true;
        }
        return false;
    }

为什么那个系统占用这么多CPU?

非常感谢您的帮助。

一个应用程序在系统上消耗几乎100%的CPU

猜测,25%是一个完整的核心,而P4(顺便说一句,更糟糕的是,MHz是比较处理器的一种糟糕的方式)是单线程的,所以它完全繁忙。

你的代码中有一个繁忙的循环(我怀疑),所以让执行CPU的一个线程一直处于繁忙状态。

在多核系统上,如果其他核可用,则其他进程将转到其他核。

当你有这个紧密的循环时,CPU的使用将继续,即使它在一个空的连接集合上迭代。要么休眠,要么不那么频繁地调用函数,要么正确地完成工作,并进行选择或轮询,以便操作系统在您有数据时唤醒您

它消耗了执行线程中所有可用的CPU:单核几乎100%,4核CPU几乎25%(它执行的核心的100%)。

正如@Douglas所指出的,这可能是一个循环的结果,或者只是你的应用程序消耗数据的速度比它们的生产速度慢)。

如果这是它的行为,那么它将使用所有可用的CPU时间。如果您不想使用所有CPU时间(因为它会减慢系统速度或消耗太多电池),则可能会降低进程优先级。不能设置System.Windows.Forms.Timer对象的优先级(它具有进程的优先级)。问题是,你真的必须经常调用这个函数吗?最小化时不能禁用吗?无论如何,我在想:应用程序消耗了100%的一个核心,只用于化妆。。。我知道改变一些有效的东西很烦人,但是。。。