简体   繁体   English

线程c#中的CPU使用率高

[英]High CPU usage in thread c#

Hi I'm having high CPU usage (15-16%) when running this thread, it is a thread that is supposed to keep looping unless "ssStop" is set to true (which works), however, while it is running I open task manager and I see that the program is using up 15% of the computers processing power, it drops back down to 1-2% once the thread has been exited. 嗨,我在运行此线程时的CPU使用率很高(15-16%),除非“ ssStop”设置为true(有效),否则该线程应该一直循环运行,但是在运行时我打开了该线程任务管理器,我看到该程序正在消耗15%的计算机处理能力,一旦退出线程,它就会下降到1-2%。 Even when using EventWaitHandle which I found searching for this problem online it still remains this high, does anyone know what I'm doing wrong here?: 即使在使用EventWaitHandle时(我发现它在网上搜索此问题)仍然保持很高的水平,有人知道我在做什么错吗?:

public void Receive()
{
    try
    {              
        bool firstTimeRun = true;
        TcpListener ssTcpListener = new TcpListener(IPAddress.Any, 1500);
        TcpClient tcpReceiver;
        ssTcpListener.Start();
        while (!ssStop)
        {
            //Start listening for connection.
            //Accept any incoming connection requests on port 1500.
            tcpReceiver = new TcpClient();
            if (ssTcpListener.Pending())
            {
                tcpReceiver = ssTcpListener.AcceptTcpClient();
            }
            if (tcpReceiver.Connected)
            {
                //looped for first time; receives whole image.
                if (firstTimeRun)
                {
                    //TCP connected. Receive images from contact
                    NetworkStream receivedNs = new NetworkStream(tcpReceiver.Client);
                    Bitmap image = new Bitmap(receivedNs);
                    receivedImage = image;
                    pboScrnShare.Image = image;
                    receivedNs.Flush();
                    firstTimeRun = false;                            
                }
                //second time or higher looped; receives difference and overlays it.
                else if (!firstTimeRun)
                {
                    NetworkStream receivedNs = new NetworkStream(tcpReceiver.Client);

                    //Put image into picturebox.
                    receivedImage2 = new Bitmap(receivedNs);
                    receivedImage2.MakeTransparent(Color.Black);
                    Bitmap overlayedImage = new Bitmap(receivedImage.Width, receivedImage.Height);

                    using (Graphics gr = Graphics.FromImage(overlayedImage))
                    {
                        gr.DrawImage(receivedImage, new Point(0, 0));
                        gr.DrawImage(receivedImage2, new Point(0, 0));
                    }


                    try
                    {
                        pboScrnShare.Image = overlayedImage;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "pbo second run");
                    }
                    receivedImage2.Dispose();
                    if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate() { receivedImage = overlayedImage; })); } else { receivedImage = overlayedImage; }
                    receivedNs.Flush();
                }
                tcpReceiver.Close();
            }
            myEventWaitHandle.WaitOne(10, true);
        }
        ssTcpListener.Stop();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), "Invited ReceiveSS()", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

You are burning cpu cycles busy polling for a connection. 您正在刻录CPU周期,忙于轮询连接。 Considering most of the time Pending is returning false, your loop is spinning around like this: 考虑到Pending返回false的大部分时间,您的循环像这样旋转:

while (!ssStop)
{
    tcpReceiver = new TcpClient();
    myEventHandle.WaitOne(10, false);
}

Now if myEventHandle is unset then the 10ms delay in WaitOne would effectively throttle execution but my guess would be that the event is set so that WaitOne immediately returns true . 现在,如果未设置myEventHandle,则WaitOne的10ms延迟将有效地限制执行,但我的猜测是该事件已设置,以便WaitOne立即返回true

It is not necessary to poll for a connection since AcceptTcpClient will block waiting for a connection. 不需要轮询连接,因为AcceptTcpClient将阻止等待连接。 So if you changed your code around a bit it should work as expected: 因此,如果您稍微更改了代码,它应该可以按预期工作:

while (!ssStop)
{
    TcpClient tcpReceiver = ssTcpListener.AcceptTcpClient(); // this blocks
    ...
}

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

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