简体   繁体   English

从 stream 获取字节数组 linux 通过 MONO 获得高 CPU 使用率

[英]Getting byte array from a stream HIGH CPU usage on linux via MONO

we are using this code on windows.. on widnows it getting 0% CPU usage we are rebuilded code run into linux debian 10 and we are running it via MONO Its getting 50%+ CPU usage:( we are using this code on windows.. on widnows it getting 0% CPU usage we are rebuilded code run into linux debian 10 and we are running it via MONO Its getting 50%+ CPU usage:(

please help, how can we solve this to get 0% cpu usage..?请帮忙,我们如何解决这个问题以获得0%的cpu使用率..? without thread sleep...没有线程睡眠...

Many thanks for your helping !!非常感谢您的帮助!!

using System;
using System.IO;
using System.Net;
using System.Threading;

namespace cpuissues
{
    public class Rec
    {
        
        public Rec()
        {
            start();
        }
        private void start()
        {
            try
            {
                Thread httP_start = new Thread(httpStart);
                httP_start.Start();
                //httpStart();

            }
            catch (Exception ex)
            {

            }
        }


        private async void httpStart()
        {
            string url = "http://tviptv.iptv-channel.ru:8000/live/NndqUU6Xoe/mV6O7VmqLx/138.ts"; //"http://tviptv.iptv-channel.ru:8000/live/NndqUU6Xoe/mV6O7VmqLx/1.ts";

            try
            {

                // Console.WriteLine("About to open Webclient");
                WebClient client = new WebClient();

                using (Stream stream = client.OpenRead(url))
                {
                    int bytesReceived = 1316; //must be used 1316 value for our purposes
                    byte[] buffer = new byte[bytesReceived];
                    int i = 0;

                    while ((bytesReceived = stream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        i = i + 1;
                        Console.WriteLine(Convert.ToString(i));
                        //Thread.Sleep(100); //DO NOT USE THIS ... ITS NOT SOLUTION we need nonstop getting data...!
                    }

                   
                }
            }
            catch (Exception e)
            {

            }
        }
    }
}

You need to actually await the read otherwise you're just constantly looping:您实际上需要等待读取,否则您只是不断循环:

Change this line:更改此行:

while ((bytesReceived = stream.Read(buffer, 0, buffer.Length)) != 0)

to this:对此:

while ((bytesReceived = await stream.ReadAsync(buffer, 0, buffer.Length)) != 0)

You should also await the OpenRead :您还应该等待OpenRead

using (Stream stream = await client.OpenReadTaskAsync(url)) 

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

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