我很难理解c# async await的基本概念。
基本上,我有一个对象列表需要处理,处理包括遍历其属性并连接字符串,然后创建一个新对象(在这种情况下称为trellocard),最终添加一个trellocards列表。
迭代需要相当长的时间,所以我想异步地处理多个对象。
我尝试了多种方法,但基本上我想做的是这样的。 (在下面的示例中,我已删除处理,并只放置system.threading.thread.sleep(200)。我等待这不是异步方法,我可以使用tasks.delay,但重点是我的处理没有任何异步方法,我想运行整个方法与多个实例。
基本上,我有一个对象列表需要处理,处理包括遍历其属性并连接字符串,然后创建一个新对象(在这种情况下称为trellocard),最终添加一个trellocards列表。
迭代需要相当长的时间,所以我想异步地处理多个对象。
我尝试了多种方法,但基本上我想做的是这样的。 (在下面的示例中,我已删除处理,并只放置system.threading.thread.sleep(200)。我等待这不是异步方法,我可以使用tasks.delay,但重点是我的处理没有任何异步方法,我想运行整个方法与多个实例。
private async Task<List<TrelloCard>> ProcessJobs(IQueryable<IGrouping<CardGrouping, Job>> jobs)
{
List<TrelloCard> cards = new List<TrelloCard>();
foreach (var job in jobs.ToList())
{
card = await ProcessCards(job, cards); // I would like to run multiple instances of the processing
cards.add(card); //Once each instance is finshed it adds it to the list
}
private async Task<TrelloCard> ProcessCards(Job job)
{
System.Threading.Thread.Sleep(2000); //Just for examples sake
return new TrelloCard();
}