Search code examples
c#.netasync-awaitclienthttpclient

why does awaiter.IsCompleted always return false?


I'm new to .NET and writing a program for test.Right now I'm using TaskAwaiter and when I get awaiter from it's instance, it always returns false on awaiter.IsCompleted and I don't know why and how to fix it? Am I using it wrong?

TaskAwaiter<HttpResponseMessage> awaiter;
str = "login.action";
awaiter = client.client.GetAsync(str).GetAwaiter();
if (awaiter.IsCompleted)
{
  break;
}

Solution

  • Am I using it wrong?

    You are assuming a HTTP request happens immediately. This is not the case. You have to await it.

    There are edge cases where the called async method may not be async - i.e. returning a cached result - and those are the ONLY cases you willl get an IsCompleted = true right after calling in.