public AsyncServiceOperation() { this.OnDone = () => { Mre.Set(); }; }
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 Stop() { lock (_lock) { if (_thread != null) { Mre.Set(); _thread.Cancel(); _thread.WaitForValue(t => t.IsRunning == false, 1000); _thread.Dispose(); _thread = null; } Client?.Disconnect(); Client = null; } }