N
N
NameOf Var2019-03-24 17:58:55
.NET
NameOf Var, 2019-03-24 17:58:55

How to implement waiting for a method to execute without using a while loop?

Hello!
I have two separate services that are somehow interconnected. To one I send requests via HTTP, and from the other I expect a response via web sockets.
It works like this:

static async void Run()
{
   await SendMessageToServer(); // отправить запрос по HTTP
   WaitResponseFromWebSocket(); // ожидать ответа по веб сокету
}

Question: how to make sure that the Run () method does not complete until the response from the web socket arrives? Is it possible to track the status of a web socket response without using a while loop?
I tried to do it through AutoResetEvent, but my Run () method is launched through Task.Run, and for some reason WaitOne () does not work in that place.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lam0x86, 2019-03-25
@hax

If I understand correctly, the websocket message handler is running somewhere else. You can create a common TaskCompletionSource and call in the Run method await tcs.Task;, and when a message arrives on the socket, call tcs.SetResult();.

P
Peter, 2019-03-24
@petermzg

Websocket can send your message in parts and in any case you will have to do while until you get the state EndOfMessage == true.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question