c# - 通过 MONO 从流中获取字节数组 linux 上的高 CPU 使用率
问题描述
我们在windows上使用这个代码..在widnows上它得到0%的CPU使用率我们重建的代码运行到linux debian 10中,我们通过MONO运行它它得到50%以上的CPU使用率:(
请帮忙,我们如何解决这个问题以获得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))
推荐阅读
- mongodb - 我如何使用mingo防止重复数据库
- python - 应用服务未生成具有新日期的文件
- java - 如何解决/禁用 java 项目中的 -Werrors?
- c# - 我如何拦截wpf(核心)动画(c#)上的每一帧
- sockets - 端口号是否意味着计算机可以拥有的连接数?
- git - 是否可以从不同的文件夹切换 git 存储库分支?
- android - Android Studio - 带回栈的导航抽屉
- javascript - chartjs 更改数据动画
- verilog - Verilog 错误处理“always”块中的两个 posedge 信号
- c# - 为什么我在 MySQL 'UPDATE' 命令上遇到错误?