C#UDP侦听器线程 - 不用刻录CPU(true)?

时间:2011-04-06 14:55:01

标签: c# sockets network-programming udp

使用以下代码,是否有更好的方法来设置UDP监听而不是while(true) 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);
        }
    }

1 个答案:

答案 0 :(得分:3)

ReceiveFrom正在阻止。线程将暂停,直到数据到达。除非Socket处于非阻止模式。