public void StartBackgroundSender() { _source = new CancellationTokenSource(); CancellationToken token = _source.Token; var task = Task.Run(() => { token.ThrowIfCancellationRequested(); ExampleUdpSender sender = new ExampleUdpSender(); sender.Connect(SenderListenerPort); while (true) { try { string data = DateTime.Now.ToString(); int sent = sender.Send(data); Console.WriteLine($"{_pid} : Sent {sent} bytes : {data}"); Thread.Sleep(1000); } catch (Exception ex) { Console.WriteLine($"{DateTime.Now.ToString("hh:mm:ss.fff")} Status Sender : {ex.Message}"); } if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); sender.Close(); sender.Dispose(); } } }, token); }
public void StartSender() { ExampleUdpSender sender = new ExampleUdpSender(); sender.Connect(SenderListenerPort); while (true) { try { string data = DateTime.Now.ToString(); int sent = sender.Send(data); Console.WriteLine($"{_pid} : Sent {sent} bytes : {data}"); Thread.Sleep(1000); } catch (Exception ex) { Console.WriteLine($"{_pid} : {ex.Message}"); } } }