Junior — Middle
Kuidas rakendada ootamist kõigi ülesannete komplekti lõpetamiseks?
sobes.tech AI
Vastus AI-lt
In C#, to wait for all tasks in a set to complete, you can use the Task.WhenAll method. This method accepts an array or collection of tasks and returns a task that completes when all the provided tasks have finished.
Example:
Task[] tasks = new Task[]
{
Task.Run(() => DoWork(1)),
Task.Run(() => DoWork(2)),
Task.Run(() => DoWork(3))
};
await Task.WhenAll(tasks);
void DoWork(int id)
{
// some work
Console.WriteLine($"Task {id} completed");
}
If you are not using async/await, you can call Task.WaitAll(tasks), which blocks the current thread until all tasks are completed.