C#UDP侦听器线程-不使用while(true)刻录CPU吗? [英] C# UDP Listener Thread -- Don't Burn CPU with while(true)?
本文介绍了C#UDP侦听器线程-不使用while(true)刻录CPU吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下代码,是否有比使用Thread.Sleep(10)
的while(true)
更好的方法来设置UDP侦听?
With the following code, is there a better way to set up a UDP listen than a while(true)
with Thread.Sleep(10)
?
public void Start()
{
socket.Bind(ip);
while (true)
{
data = new byte[1024];
receivedDataLength = socket.ReceiveFrom(data, ref Remote);
raw = Encoding.ASCII.GetString(data, 0, receivedDataLength);
row = new LogRow(raw);
//Console.WriteLine(row.ClientIp);
row_queue.Enqueue(row);
Thread.Sleep(10);
}
}
推荐答案
ReceiveFrom
正在阻止.线程将被挂起,直到数据到达.除非Socket
处于非阻止模式.
ReceiveFrom
is blocking. Thread will be suspended until data arrives. Unless Socket
is in non-blocking mode.
这篇关于C#UDP侦听器线程-不使用while(true)刻录CPU吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文