public void StartReceive() { while (true) { var stream = _tcpClient.GetStream(); stream.Read(_messageLenght, 0, _messageLenght.Length); var data = new byte[BitConverter.ToInt32(_messageLenght, 0)]; stream.Read(data, 0, data.Length); var message = new Message(); message.ReadBytes(data); if (MessageReceived != null) { var thread = new Thread(() => MessageReceived(this, message)); thread.Start(); } } }
async public Task StartReceiveAsync() { while (true) { var stream = _tcpClient.GetStream(); var mesLen = stream.ReadAsync(_messageLenght, 0, _messageLenght.Length); await mesLen; //mesLen.Result()' var data = new byte[BitConverter.ToInt32(_messageLenght, 0)]; await stream.ReadAsync(data, 0, data.Length); var message = new Message(); message.ReadBytes(data); if (MessageReceived != null) { //var thread = new Thread(() => MessageReceived(this, message)); //thread.Start(); //await MessageReceivedAsync(this, message); await Task.Run(() => MessageReceived(this, message)); } } }
public void StartReceive() { while (true) { var stream = _tcpClient.GetStream(); stream.Read(_messageLenght, 0, _messageLenght.Length); var data = new byte[BitConverter.ToInt32(_messageLenght, 0)]; stream.Read(data, 0, data.Length); var message = new Message(); message.ReadBytes(data); if (MessageReceived != null) { var thread = new Thread(() => MessageReceived(this, message)); thread.Start(); } } }
async public Task StartReceiveAsync() { while (true) { var stream = _tcpClient.GetStream(); var mesLen = stream.ReadAsync(_messageLenght, 0, _messageLenght.Length); await mesLen; //mesLen.Result()' var data = new byte[BitConverter.ToInt32(_messageLenght, 0)]; await stream.ReadAsync(data, 0, data.Length); var message = new Message(); message.ReadBytes(data); if (MessageReceived != null) { //var thread = new Thread(() => MessageReceived(this, message)); //thread.Start(); //await MessageReceivedAsync(this, message); await Task.Run(() => MessageReceived(this, message)); } } }