Пример #1
0
 private void ThreadProc(CancellationTokenSource cts)
 {
     try
     {
         while (!cts.IsCancellationRequested)
         {
             if (TcpHelpers.IsMessageAvailable(_client, OnException))
             {
                 TcpHelpers.ReadMessageAsync(_stream, _receiveBufferSize, (bytes, exception) =>
                 {
                     if (exception != null)
                     {
                         OnException?.Invoke(exception);
                     }
                     if (bytes != null)
                     {
                         _messages.Enqueue(bytes);
                     }
                 }, cts).Wait();
             }
             System.Threading.Thread.Sleep(1); //play nice with other threads
         }
         cts.Dispose();
     }
     catch (Exception e)
     {
         Log.e("MessageReader Exception: {0}", e.Message);
     }
     _thread = null;
 }
Пример #2
0
 private void ThreadProc(CancellationTokenSource cts)
 {
     try
     {
         while (!cts.IsCancellationRequested)
         {
             if (!_sendQueue.IsEmpty && _stream != null)
             {
                 byte[] message;
                 if (_sendQueue.TryDequeue(out message))
                 {
                     TcpHelpers.SendMessageAsync(_stream, _sendBufferSize, message, exception =>
                     {
                         if (exception != null)
                         {
                             OnException?.Invoke(exception);
                         }
                         else
                         {
                             OnMessageSent?.Invoke();
                         }
                     }, cts).Wait();
                 }
             }
             System.Threading.Thread.Sleep(1);
         }
         cts?.Dispose();
     }
     catch (Exception e)
     {
         Log.e("MessageReader Thread Exception: {0}", e.Message);
     }
     _thread = null;
 }