public void StartBackgroundListener() { _source = new CancellationTokenSource(); CancellationToken token = _source.Token; var task = Task.Run(() => { token.ThrowIfCancellationRequested(); ExampleUdpListener listener = new ExampleUdpListener(); listener.Connect(SenderListenerPort); while (true) { try { string data = listener.Read(true); Console.WriteLine($"{_pid} : Read {data.Length} bytes : {data}"); } catch (Exception ex) { Console.WriteLine($"{DateTime.Now.ToString("hh:mm:ss.fff")} Status Sender : {ex.Message}"); } if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); listener.Close(); listener.Dispose(); } } }, token); }
public void StartListener() { ExampleUdpListener listener = new ExampleUdpListener(); listener.Connect(SenderListenerPort); while (true) { try { string data = listener.Read(true); Console.WriteLine($"{_pid} : Read {data.Length} bytes : {data}"); } catch (Exception ex) { Console.WriteLine($"{_pid} : {ex.Message}"); } } }