7

I have following methods which are working fine for me:

1.This is using "async await" approach:

public async Task AddClients(string clientId)
{
   bool isAlreadyAdded = await CheckIsClientExists(clientId);
   if(isAlreadyAdded)
   {
     await AddNewClientToGroup(clientId);
   }
}

2.This is method is using GetAwaiter() approach:

public async Task AddClients(string clientId)
{
   bool isAlreadyAdded = await CheckIsClientExists(clientId);
   if(isAlreadyAdded)
   {
      AddNewClientToGroup(clientId).GetAwaiter().GetResult();
   }
}

I have seen the link : Async await vs GetAwaiter().GetResult() and callback . And this have not explained why and how GetAwaiter() leads to a deadlock situation.

Can anyone explain the difference between the both approaches, i.e await and GetAwaiter()?

Pranay Deep
  • 1,371
  • 4
  • 24
  • 44
  • 5
    Well `GetResult()` blocks, whereas `await` doesn't... normally `GetResult` is called automatically by the compiler in the generated code, after it knows the operation has completed – Jon Skeet Sep 25 '17 at 14:08
  • Sir @JonSkeet , thanks for the quick answer. But if possible can you provide the more detailed answer with example. I am learning "async await" on my own. – Pranay Deep Sep 25 '17 at 14:13
  • 3
    No, explaining how async/await works is far too broad for Stack Overflow - there are lots of articles about it online though. (Or a whole chapter in my C# in Depth book.) – Jon Skeet Sep 25 '17 at 14:21
  • Ok thanks. I will read it again. Then please answer you comment, i'll accept so that if other new bee bump into this he/she will get correct answer. – Pranay Deep Sep 25 '17 at 14:22
  • 3
    No, because a) that wouldn't be an actual answer; b) this question is already closed as a duplicate of another. – Jon Skeet Sep 25 '17 at 14:27

0 Answers0