简体   繁体   中英

Await not freeing up the calling thread in async method

I'm printing out the Messages property string list in AsyncDemoViewModel.

If i use Thread.Sleep(1000) in GetVideosSlowlyAsync() the output is

Time spent: 4001 milliseconds 

 - Started on thread 18, finished on thread 18  
 - Started on thread 18, finished on thread 18  
 - Started on thread 18, finished on thread 18 
 - Started on thread 18, finished on thread 18

If i use await Task.Delay(1000) in GetVideosSlowlyAsync() the output is

Time spent: 4053 milliseconds 


 - Started on thread 12, finished on thread 19 
 - Started on thread 19, finished on thread 16 
 - Started on thread 16, finished on thread 19 
 - Started on thread 19, finished on thread 9

Why is the await call not freeing up the calling thread ? I was expecting the await version to be about 4x faster in completion time.

Controller code:

public class AsyncDemoController : Controller
{
    private DepartmentDb _db;

    public AsyncDemoController(DepartmentDb db)
    {
        _db = db; 
    }

    public async Task<ActionResult> Index()
    {

        var sw = Stopwatch.StartNew();
        var v1 = await GetVideosSlowlyAsync();
        var v2 = await GetVideosSlowlyAsync();
        var v3 = await GetVideosSlowlyAsync();
        var v4 = await GetVideosSlowlyAsync();

        var vm = new AsyncDemoViewModel() {Videos = v1.Item2, Messages = new List<string>()};
        sw.Stop();
        vm.Messages.Add(string.Format("Time spent: {0} milliseconds", sw.ElapsedMilliseconds));
        vm.Messages.Add(v1.Item1);
        vm.Messages.Add(v2.Item1);
        vm.Messages.Add(v3.Item1);
        vm.Messages.Add(v4.Item1);

        return View(vm);
    }

    private async Task<Tuple<string, IEnumerable<Video>>> GetVideosSlowlyAsync()
    {
        var t1 = Thread.CurrentThread.ManagedThreadId;
        await Task.Delay(1000); // Thread.Sleep(1000);
        var t2 = Thread.CurrentThread.ManagedThreadId;
        return Tuple.Create(string.Format("Started on thread {0}, finished on thread {1}", t1, t2), _db.Videos.AsEnumerable());
    }

}

The await method wait's for the GetVideosSlowlyAsync method to complete. You need to move the await to the place where the result of the operation is needed:

public async Task<ActionResult> Index()
{

    var sw = Stopwatch.StartNew();
    var v1 = GetVideosSlowlyAsync();
    var v2 = GetVideosSlowlyAsync();
    var v3 = GetVideosSlowlyAsync();
    var v4 = GetVideosSlowlyAsync();

    var vm = new AsyncDemoViewModel() {Videos = (await v1).Item2, Messages = new List<string>()};
    sw.Stop();
    vm.Messages.Add(string.Format("Time spent: {0} milliseconds", sw.ElapsedMilliseconds));
    vm.Messages.Add((await v1).Item1);
    vm.Messages.Add((await v2).Item1);
    vm.Messages.Add((await v3).Item1);
    vm.Messages.Add((await v4).Item1);

    return View(vm);
}

You are calling await GetVideosSlowlyAsync() which will return to the calling method until the task finishes. If you want to see the results you are expecting you need to remove this await. This way it will start all 4 GetVideosSlowlyAsync() one after another while the Task.Delay() method is sleeping on the thread pool.

Try something like this,

List<Task> taskList = new List<Task>();
taskList.Add(GetVideosSlowlyAsync());
taskList.Add(GetVideosSlowlyAsync());
taskList.Add(GetVideosSlowlyAsync());
taskList.Add(GetVideosSlowlyAsync());

Task.WaitAll(taskList.ToArray())
foreach (Task task in taskList)
    vm.Messages.Add(task.Result.Item1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM