0

I need to make an foreach loop with 3 tasks, this needs to wait till all 3 tasks are finish and than move to next one. Something like

foreach (class r in sets)
{
    Task.Factory.StartNew(() => {
      DoThisFunction1();
    }, TaskCreationOptions.LongRunning);
    Task.Factory.StartNew(() => {
     DoThisFunction2();
    }, TaskCreationOptions.LongRunning);
    Task.Factory.StartNew(() => {
     DoThisFunction3();
    }, TaskCreationOptions.LongRunning);
 }

somebody can give a simple way how to do this?

T-Heron
  • 5,385
  • 7
  • 26
  • 52
user3763117
  • 327
  • 1
  • 5
  • 18

2 Answers2

5

You can use WaitAll which has no return type and will block simular to Wait on a task or WhenAll which will return an awaitable Task.

Example:

var tasks = new Task[] {
    Task.Factory.StartNew(() => {
        DoThisFunction1();
    }, TaskCreationOptions.LongRunning),
    Task.Factory.StartNew(() => {
        DoThisFunction2();
    }, TaskCreationOptions.LongRunning),
    Task.Factory.StartNew(() => {
        DoThisFunction3();
    }, TaskCreationOptions.LongRunning)
};

Task.WaitAll(tasks);
// or
await Task.WhenAll(tasks);

A more detailed answer can be found here

NtFreX
  • 10,379
  • 2
  • 43
  • 63
  • That simple? Wow, was looking for this quite sometime... thanks! – user3763117 Dec 10 '16 at 19:14
  • BTW, this is very low quality answer compared to standard duplicate... – Alexei Levenkov Dec 10 '16 at 19:33
  • @AlexeiLevenkov Should I improve it according to the sample you provided or does this not matter because this is a dublicate? – NtFreX Dec 10 '16 at 19:34
  • 1
    @Dr.Fre If you have better answer than already present in the linked duplicate - indeed provide it to the [duplicate question](http://stackoverflow.com/questions/25009437/running-multiple-async-tasks-and-waiting-for-them-all-to-complete), otherwise I don't think it worth your time to improve this one (which by itself is fine, just not much more useful than existing once) – Alexei Levenkov Dec 10 '16 at 19:38
2
class TasksTest
{
    public void Test()
    {
        List<string> sets = new List<string>
        {
            "set1", "set2", "set3", "set4",
        };

        foreach (var s in sets)
        {
            Console.WriteLine("Set {0}", s);
            var tasks = new[]
            {
                Task.Factory.StartNew(() => {DoThisFunction1();}, TaskCreationOptions.LongRunning),
                Task.Factory.StartNew(() => { DoThisFunction2(); }, TaskCreationOptions.LongRunning),
                Task.Factory.StartNew(() => { DoThisFunction3(); }, TaskCreationOptions.LongRunning),
            };
            Task.WaitAll(tasks);
            Console.WriteLine("End Set {0}\n------------", s);
        }
    }

    void DoThisFunction1()
    {
        Thread.Sleep(1000);
        Console.WriteLine("F1");
    }
    void DoThisFunction2()
    {
        Thread.Sleep(1500);
        Console.WriteLine("F2");
    }
    void DoThisFunction3()
    {
        Thread.Sleep(2000);
        Console.WriteLine("F3");
    }
}