Looping Async Task List in C#

后端 未结 4 1175
半阙折子戏
半阙折子戏 2021-01-22 07:26

I am trying to parse data from several websites continuously. I would like this action to be preformed individually in a loop in an asynchronous manner until the program is clos

4条回答
  •  旧时难觅i
    2021-01-22 07:53

    If you want to visit the site again as soon as it is complete, you probably want to use Task.WhenAny and integrate your outer loop with your inner loop, something like this (assuming the ParseData function will return the Site it is parsing for):

    async public void ParseAll(List SiteList)
    {
        while (true)
        {
            List> TaskList = new List>();
    
            foreach(Site s in SiteList)
            {
                TaskList.Add(s.ParseData());
            }
    
            await Task.WhenAny(TaskList);
            TaskList = TaskList.Select(t => t.IsCompleted ? t.Result.ParseData() : t).ToList();
        }
    }
    

提交回复
热议问题