Task.Wait() vs. await

Have an Opinion?

  1. Hi,

    “await asyncTest.ProcessAsync2(null);” will not throw an exception unless you add Thread.Sleep(200);

    The reason is that the program is finished execution before the async operion raise an exception

    1. Hi Boaz,
      Actually no. It will throw the exception without any need for “Thread.Sleep()” since “await” itself does the wait. I suspect that the issue you are seeing is that you are not waiting for the asynchronous method encapsulating the “await” itself to finish, thus causing the program to terminate before it reaches this line.
      As a side note, please be aware that using “Thread.Sleep()” for synchronization is not a good practice. It would have been better to do something like this:

      static void Main()
      {
      Do().GetAwaiter().GetResult();
      }

      static async Task Do()
      {
      var asyncTest = new AsyncClass();
      await asyncTest.ProcessAsync2(null);

      }

  2. HI,

    You wrote:
    “…and it does require the await keyword which is limited only to methods marked with async…”

    Surely it should be:

    “…and it does not require the await keyword which is limited only to methods marked with async….”

Read Next

Sliding Sidebar