Task.Factory.StartNew with async lambda and Task.WaitAll

后端 未结 4 1524
小鲜肉
小鲜肉 2020-11-27 07:59

I\'m trying to use Task.WaitAll on a list of tasks. The thing is the tasks are an async lambda which breaks Tasks.WaitAll as it never waits.

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 08:24

    you have to use the Task.ContinueWith method. Like this

    List tasks = new List();
    tasks.Add(Task.Factory.StartNew(() =>
    {
        using (dbContext = new DatabaseContext())
        {
            return dbContext.Where(r => r.Id = 100).ToListAsync().ContinueWith(t =>
                {
                    var records = t.Result;
                    // do long cpu process here...
                });
            }
        }
    }
    

提交回复
热议问题