private async void Listen() { await Task.Run(async() => { var wait = new SpinWait(); while (IsActive) { var receiveResult = await _client.ReceiveAsync(); var eventArgs = new DatagramReceivedArgs(receiveResult.Buffer, receiveResult.RemoteEndPoint); DatagramReceived?.Invoke(this, eventArgs); if (eventArgs.Reply != null) { await _client.SendAsync(eventArgs.Reply, eventArgs.Reply.Length, eventArgs.RemoteEndpoint); } wait.SpinOnce(); } }); }
private async void Listen() { await Task.Run(async() => { using (var client = new UdpClient(Port)) { while (IsActive) { var receiveResult = await client.ReceiveAsync(); var eventArgs = new DatagramReceivedArgs(receiveResult.Buffer, receiveResult.RemoteEndPoint); DatagramReceived?.Invoke(this, eventArgs); if (eventArgs.Reply != null) { await client.SendAsync(eventArgs.Reply, eventArgs.Reply.Length, eventArgs.RemoteEndpoint); } } } }); }