0

I'm writing a simplest caller console app according to this article with core3.1. When i changed channel.Open().Wait(5000); to await channel.Open();, it's stucked at line proxy.Ping();.

And this is also found on a callee:

Task openTask = channel.Open();
openTask.Wait();

can't replace with await channel.Open();

Task<IAsyncDisposable> registrationTask = realm.Services.RegisterCallee(instance);
registrationTask.Wait();

can't replace with await realm.Services.RegisterCallee(instance);

Once await is used, the program always gets stuck at line proxy.Ping();

The solution zipfile.

Why?Isn't await waiting?

zhusp
  • 131
  • 12
  • "can't replace", why not? If you mix `await` and `.Wait`, most likely one is blocking the other, you're getting deadlocked. Proper use of `.Wait` is difficult to achieve, and it might be that you introducing `await` in one or two places have upset a delicate balance of timing. – Lasse V. Karlsen Jul 10 '20 at 07:38
  • I don't seem to mix await and wait.Maybe it's not easy for me to understand. – zhusp Jul 10 '20 at 09:31
  • You will have to provide more code for this question to be answerable. It might be that if it works with `.Wait` but not with `await` it might be that the objects involved might be thread-bound, which means they belong to a certain thread and only work on that thread. – Lasse V. Karlsen Jul 10 '20 at 09:50
  • I've packaged and uploaded the entire solution.[zip](https://1drv.ms/u/s!AqxJcIfHZ18Udy4EV7O4OxuJzFs?e=oZ6cKT). Thanks. – zhusp Jul 10 '20 at 10:10
  • Your ping method is blocking the WebSocket thread. Switch to the `PingAsync` format (recommended approach) or else add an `await Task.Yield()` before the call to Ping in order to release the WebSocket thread (less recommended). – darkl Jul 11 '20 at 01:58
  • @dakl I modified it and it still doesn't work.[modified file](https://1drv.ms/u/s!AqxJcIfHZ18Udy4EV7O4OxuJzFs?e=zvZ7jn) – zhusp Jul 11 '20 at 07:16
  • Your `Console.Readline()` in the Calle Program.cs blocks the WebSocket thread. Add an `await Task.Yield()` before it. – darkl Jul 11 '20 at 10:04
  • @darkl It's done.THS. I can't accept a answer in the comments. Can you formally answer this question? – zhusp Jul 11 '20 at 10:55

1 Answers1

1

Your method call proxy.Ping() blocks the Websocket thread. If you use async syntax, you should be careful not to block the Websocket thread. Use the await proxy.PingAsync() version. If you need to make blocking calls like Console.ReadLine(), make sure to release the Websocket thread by calling await Task.Yield() before making your blocking call.

darkl
  • 569
  • 2
  • 13