I have threads:
- UI thread with Dispatcher loop
- background thread that listens for messages in a queuing framework.
when a message is received, an event is fired in the background thread:
messageReceiver.Received += (sender, args) => ...
In UI thread I would like to await a message, something like this:
void ButtonClicked(object sender, RoutedEventArgs e) {
await NextMessage(); //should return when messageReceiver.Received is fired
}
How to implement awaitable NextMessage method, so it does not create new thread each time?
There is SemaphoreSlim
class where I can await WaitAsync, but is seems to create new thread that is blocked until SemaphoreSlim is released.
Maybe TaskCompletionSource
is the way to go? Which TaskCreationOption should I use then?