Oleg Mishenkin Oleg Mishenkin - 1 year ago 105
C# Question

Async method .Result vs Sync method

Does this two methods works the same way? Both of this block main thread, isn't?

// 1. Use async method
public IEnumerable<Entity> AsyncMethod()
{
return Context.Entities.ToListAsync().Result;
}

// 2. Use sync method
public IEnumerable<Entity> SyncMethod()
{
return Context.Entities.ToList();
}

Answer Source

Does this two methods works the same way?

Pretty much, the second approach has one less context switch.

Both of this block main thread, isn't?

Correct

First approach ToList is async and Result would block until the ToList task had completed. Second approach is not async.

When it come to just ToList, using Result may not make much sense.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download