c#TCP协议通讯是否可以实现触发事件的形式接收消息
请教一下各位,c#TCP协议通讯是否可以实现触发式的接收消息?因为我在很多文章中看到都是利用死循环的形式来接收消息如:
result = new byte[1024];
while (true)
{
try
{
stream.Read(result, 0, result.Length);
this.txt.Text += Encoding.ASCII.GetString(result).Trim();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
是否可以实现利用触发事件的形式来接收事件比如串口通讯的接收事件:
ComServiceInterface.downSp.DataReceived += new SerialDataReceivedEventHandler(revComData);//配置接收串口的触发事件