await
is not Wait
. It is unclear what the code is that is calling StartListening()
, but one option is to await
it, as suggested:
await StartListening();
However, in some other cases it may be better to do nothing at all:
StartListening(); // drop the Task on the floor
or perhaps use ContinueWith
for a manual continuation. Since the StartListening
method catches any exceptions, there isn't anything wrong with just ignoring the returned Task
- so what you had already. I would suggest calling it StartListeningAsync
, though.
The reason for the deadlock is that if you use Wait
, your UI thread blocks waiting on an asynchronous method to complete, but that asynchronous method is capturing the sync-context, which means in order to process each continuation it tries to get onto the UI thread - which is blocked... on it.