gpt4 book ai didi

c# - 在 A SYSTEM 上消耗几乎 100% CPU 的应用程序

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:12 26 4
gpt4 key购买 nike

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

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

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

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

    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?

非常感谢任何帮助。

最佳答案

据推测,这 25% 是一个完整的内核,而 P4(顺便说一句更糟糕 - MHz 是比较处理器的糟糕方式)是单线程的,所以它非常忙。

您的代码中有一个繁忙的循环(我怀疑),因此一直保持一个 CPU 执行线程繁忙。

在多核系统上,其他进程将继续运行到其他可用的核心。

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

关于c# - 在 A SYSTEM 上消耗几乎 100% CPU 的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com