diff --git a/MySolution/ConsoleApp/TelnetSamples/AsyncSocketTelnetClient.cs b/MySolution/ConsoleApp/TelnetSamples/AsyncSocketTelnetClient.cs index 3c746d9..d76d8de 100644 --- a/MySolution/ConsoleApp/TelnetSamples/AsyncSocketTelnetClient.cs +++ b/MySolution/ConsoleApp/TelnetSamples/AsyncSocketTelnetClient.cs @@ -50,10 +50,11 @@ namespace ConsoleApp.TelnetSamples byte[] readBuffer = new byte[1024]; while (true) { - if (_cts != null && _cts.IsCancellationRequested) + //Thread.Sleep(100); + if (_cts.IsCancellationRequested) break; - int bytesRead = _cts != null && _cts.IsCancellationRequested ? 0 : await _socket.ReceiveAsync(readBuffer); + int bytesRead = await _socket.ReceiveAsync(readBuffer); if (bytesRead < 1) { if (sb.Length > 0 && this.MessageCallback != null) diff --git a/MySolution/ConsoleApp/TelnetSamples/AsyncStreamTelnetClient.cs b/MySolution/ConsoleApp/TelnetSamples/AsyncStreamTelnetClient.cs index c892133..5d6f5c6 100644 --- a/MySolution/ConsoleApp/TelnetSamples/AsyncStreamTelnetClient.cs +++ b/MySolution/ConsoleApp/TelnetSamples/AsyncStreamTelnetClient.cs @@ -9,6 +9,8 @@ namespace ConsoleApp.TelnetSamples { internal class AsyncStreamTelnetClient : IAsyncTelnetClient { + private CancellationTokenSource _cts = new CancellationTokenSource(); + public event EventHandler MessageCallback; public event EventHandler ErrorCallback; @@ -25,7 +27,8 @@ namespace ConsoleApp.TelnetSamples await _client.ConnectAsync(ip, port); _stream = _client.GetStream(); - Task.Run(ReadAsync); + _cts = new CancellationTokenSource(); + Task.Run(ReadAsync, _cts.Token); } catch (Exception) { @@ -41,6 +44,10 @@ namespace ConsoleApp.TelnetSamples byte[] readBuffer = new byte[1024]; while (true) { + Thread.Sleep(50); + if (_cts.IsCancellationRequested) + break; + int bytesRead = await this._stream.ReadAsync(readBuffer); if (bytesRead < 1) break; @@ -95,7 +102,7 @@ namespace ConsoleApp.TelnetSamples _stream.Close(); } - + _cts.Cancel(); _client.Close(); _client.Dispose(); diff --git a/MySolution/SerialComApp/SerialCommApp.resx b/MySolution/SerialComApp/SerialCommApp.resx index 4d53742..74f57dc 100644 --- a/MySolution/SerialComApp/SerialCommApp.resx +++ b/MySolution/SerialComApp/SerialCommApp.resx @@ -121,7 +121,7 @@ iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAL - EQAACxEBf2RfkQAAAsBJREFUaEPt2cnLTXEcx/FrzJCphJ0F2diIbEQokWFnzhClyFB6yEJEyh+gUMaU + EAAACxABrSO9dQAAAsBJREFUaEPt2cnLTXEcx/FrzJCphJ0F2diIbEQokWFnzhClyFB6yEJEyh+gUMaU WdlYWLBRiszjAmFjaUgZMvP+LE6dTp9z7jnnnt990PnUa/N07+/7u/ee3/g06tSpU+efzihswFncxzt8 x1e8xm0cx2oMx1+R7liOG/hdwC9cxXx0Q6dkNp7DdbCIh5iKtqUvjsF1piz9InvQE0EzDPfgOlGFKxiE IBmKZ3CF03zBt8TfmrmFAag0fXAXrmCcXrMd4zAYUYZgInbjCdx74y6h0sF9BK5QRANxJvKkKxaj2QSw