Async/await will only improve performance if it C# with Example



Async/await will only improve performance if it C# with Example

allows the machine to do additional work 
Consider the following code: 
 

public async Task MethodA() 
{ 
await MethodB(); 
// Do other work 
} 
public async Task MethodB() 
{ 
await MethodC(); 
// Do other work 
} 
public async Task MethodC() 
{ 
// Or await some other async work 
await Task.Delay(100); 
} 
This will not perform any better than 
public void MethodA() 
{ 
MethodB(); 
// Do other work 
} 
public void MethodB() 
{ 
MethodC(); 
// Do other work 
} 
public void MethodC() 
{ 
Thread.Sleep(100); 
} 
The primary purpose of async/await is to allow the machine to do additional work - for example, to allow the calling 
thread to do other work while it's waiting for a result from some I/O operation. In this case, the calling thread is 
never allowed to do more work than it would have been able to do otherwise, so there's no performance gain over 
simply calling MethodA(), MethodB(), and MethodC() synchronously. 

0 Comment's

Comment Form