0

Im trying to run a list of HttpRequest tasks parallelly (not just asynchronously), and Only after all tasks are completed- output results. But when i run my code it doesnt wait but run the whole program and then later on i see the completion of tasks(thats when responses come). why ? how do i make the writeline in the end, Wait till all tasks are done ? (and no im not looking for WhenAll(); that isn't parallel (parallelly the requests finish faster since they're all starting parallelly without awaiting each other; which is good. but again i need to print results Only AFTER they're all complete; which is where im stuck at how to do it

Stopwatch stopWatch = new Stopwatch();
List<Task> tasks = new List<Task>();

            foreach (var item in uriRequests)
            {
                tasks.Add(HttpRequestAsync(item));
            }

            stopWatch.Start();
            Parallel.ForEach(tasks, task =>
            {
                task.ConfigureAwait(false);
                
            });
            stopWatch.Stop();
            //====================i want to wait until the tasks are completed-only then output the next code (writeline) but it doesnt happen that way
            Console.WriteLine($"Finish Executing within {stopWatch.Elapsed}. Requests Completed: {completedRequests}");
  • Do not mix `Parallel` with `Task`s. They don't get along well. – Fildor Jun 15 '22 at 15:21
  • What is the size of your list of requests? It doesn't make much sense to start 1000s of Tasks in parallel. If it's only 4 or 5, it's no problem, though. If you want to wait for a Task to finish, you need to await it. If you want to await N tasks, you can use `await Task.WhenAll( taskList );` – Fildor Jun 15 '22 at 15:24
  • `await Task.WhenAll(tasks);` – Charlieface Jun 15 '22 at 15:24
  • Mind that using `await`, you need to make the caller use `async`. Which makes its caller use await ... "go async all the way". – Fildor Jun 15 '22 at 15:25
  • Use ```.Wait()``` at the end of the ```ForEach```. But @Fildor is right , if you have too many tasks in the list you do nothing. Because the parallel foreach is using multi threads approach. – Darkk L Jun 15 '22 at 15:27
  • @DarkkL _Never_ use `Wait()`. (Until you know what you are doing and _have to_ and even then it's a bad idea, really). – Fildor Jun 15 '22 at 15:32
  • i do have 1000+ requests to send, and need to wait for their completion- its also why i wanted to make it parallel so they'll be sent faster and then all i have to do is just wait for them to complete – MTX20200 Jun 15 '22 at 15:33
  • If so, I'd recommend to use TPL DataFlow to handle them in (batched) parallel. If you start all of them at once, it's no good. And mind: async != parallel and parallel is not always faster. If you send 1000s of HTTP Requests in parallel, you basically DoS yourself. – Fildor Jun 15 '22 at 15:35
  • So @Fildor is already said all you need check the answer from https://stackoverflow.com/questions/12337671/using-async-await-for-multiple-tasks and you can make it better – Darkk L Jun 15 '22 at 15:36

0 Answers0