簡體   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:(

請幫忙,我們如何解決這個問題以獲得0%的cpu使用率..? 沒有線程睡眠...

非常感謝您的幫助!!

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)
            {

            }
        }
    }
}

您實際上需要等待讀取,否則您只是不斷循環:

更改此行:

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

對此:

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

您還應該等待OpenRead

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM