public void Start(IPEndPoint remoteEndPoint) { if (Client != null) { return; } Client = new Net.BlockingSocketClient(); Client.Disconnected += (c, ep, e) => { Exception.Value = e; }; Client.MessageReady += (c) => { lock (_lock) { if (c.MessageCount > 0) { var response = c?.GetNextMessage(); if (response == null) { System.Diagnostics.Debugger.Break(); } var message = response.AsString(); if (message == Request.Value.ToUpper()) { Responses.Value += 1; } else { Exception.Value = new System.Exception("Message Lost"); } } Mre.Set(); } }; Client.ConnectAsync(remoteEndPoint).ContinueWith(task => { Mre.Reset(); _thread = W.Threading.Thread.Create(cts => { while (!cts.IsCancellationRequested) { if (Client.IsConnected) { Request.Value = string.Format("Message: {0}", Requests); Requests.Value += 1; Mre.Reset(); Client.Send(Request.Value.AsBytes()); Mre.Wait(); } //W.Threading.Thread.Sleep(1); } if (Client.IsConnected) { Client.Disconnect(); } }); }); }
public void WaitCompletion() { Mre.WaitOne(); if (Exception != null) { Assert.Fail(Exception.ToString()); } Mre.Reset(); }