Why the UI thread hangs in me when I do While , well, it was understandable if I had done this cycle in the usual method, so I made the method asynchronous and still hang, why?

 private async void button_Click(object sender, RoutedEventArgs e) { while (true) // С этим while'ом работает всё ок { Task processTask = Process(lastclient); } } async Task Process(TcpClient result) { ... тут что-то происходит while (true) // С этим while'ом всё повисает { result.Client.Receive(recievebytes); } } 

UPD 1 :

  SocketAsyncEventArgs eventArgObjectForPool = new SocketAsyncEventArgs(); eventArgObjectForPool.UserToken = users.Last<TcpClient>().Client; result.Client.ReceiveFromAsync(eventArgObjectForPool); //Вот тут вылетает, какого то параметра не хватает внутри eventArgObjectForPool 

UPD 2 :

  TcpListener listener; TcpClient user; private async void button_Click(object sender, RoutedEventArgs e) //По нажатии на клавишу запускаем наш серв { TcpListener listener = new TcpListener(listenPort); listener.Start(); TcpClient user = await listener.AcceptTcpClientAsync() while (true) { Process(user); //Если клиент подключён, то начинаем с ним работать } } async Task Process(TcpClient result) { //Тут настраиваем слушалку для клиента SocketAsyncEventArgs eventArgObjectForPool = new SocketAsyncEventArgs(); eventArgObjectForPool.UserToken = user.Client; eventArgObjectForPool.RemoteEndPoint = user.Client.RemoteEndPoint; eventArgObjectForPool.Completed += EventArgObjectForPool_Completed; listener.Server.ReceiveFromAsync(eventArgObjectForPool); // Вот это я как понимаю ставлю обработчик на входящие сообщения } private void EventArgObjectForPool_Completed(object sender, SocketAsyncEventArgs e) { Console.WriteLine("ads"); // Если что-то приходит от клиента, то выводим в консоль строку какую-нибудь. } 
  • Where is the return or break? - Alexsandr Ter
  • one
    Don't you think TcpClient user = await listener.AcceptTcpClientAsync() should have been inserted inside while ? - VladD
  • one
    async Task Process somehow too synchronous. Where is await? - Qwertiy

2 answers 2

result.Client.Receive(recievebytes); - this is a synchronous call. Of course, the UI thread will hang - it also reads synchronously from the socket in an infinite loop!

Or find the asynchronous version of the Receive method:

 async Task Process(TcpClient result) { while(true) { count += await result.Client.ReceiveAsync(recievebytes); } } 

Or make reading in a separate thread.

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

win10 universal app, async delay

await listener.Server.ReceiveFromAsync (eventArgObjectForPool) .ConfigureAwait (false);

And the `await` statement hangs in the window application / program hangs when you call Task.Result or Wait

Yet again

 TcpClient user = await listener.AcceptTcpClientAsync() 

will be executed in the UI thread. It is necessary?

 TcpClient user = await listener.AcceptTcpClientAsync().ConfigureAwait(false); 

And further execution will be in a stream different from UI.

  • Where do I get eventargobjectforpool ? - alex-rudenkiy
  • NetworkStream has a ReadAsync method msdn.microsoft.com/ru-ru/library/… if size transfer is provided, then you can organize all events yourself - Serginio
  • Well, I will try - alex-rudenkiy