locked
How to wait for the next batch until the first batch finished RRS feed

  • Question

  • User-2017229834 posted

    Hi Everyone,

    I have created four async method, task1(), task2(), task3() and task4(). And one AsyncMain() Method to call these task. I want to call these method in batch - wise. like second batch will run only after the first batch completed. My code is here

    class Program
        {
     
           public async Task<string> task1(int i)
            {
                Task<string> t = new Task<string>
                (() =>
                {
                    for (int i1 = 0; i1 < 1000; i1++)
                    {
    
                    }
                    return "task1";
                });
                t.Start();
                string result = await t;
                Console.WriteLine(result + " in batch :" + i);
                return result;
            }
            public async Task<string> task2(int i)
            {
                Task<string> t = new Task<string>
                (() =>
                {
                    for (int i2 = 0; i2 < 100000; i2++)
                    {
    
                    }
                    return "task2";
                });
                t.Start();
                string result = await t;
                Console.WriteLine(result + " in batch :" + i);
                return result;
            }
            public async Task<string> task3(int i)
            {
                Task<string> t = new Task<string>
                (() =>
                {
                    for (int i3 = 0; i3 < 100; i3++)
                    {
    
                    }
                    return "task3";
                });
                t.Start();
                string result = await t;
                Console.WriteLine(result + " in batch :" + i);
                return result;
            }
            public async Task<string> task4(int i)
            {
                Task<string> t = new Task<string>
                (
                    () =>
                    {
                        for (int i4 = 0; i4 < 50000; i4++)
                        {
    
                        }
                        Thread.Sleep(5000);
                        return "task4";
                    }
                );
                t.Start();
                string result = await t;
                Console.WriteLine(result + " in batch :" + i);
                return result;
            }
    
            public async void MainAsync()
            {
                var items = new[] { 1, 2, 3, 4 };
                SemaphoreSlim maxThread = new SemaphoreSlim(1);
                for (int i = 1; i <= items.Length; i++)
                {
                    maxThread.Wait();
                    await Task.Factory.StartNew(() =>
                    {
                        Task.Run(() => task1(i));
                        Task.Run(() => task2(i));
                        Task.Run(() => task3(i));
                        Task.Run(() => task4(i));
                    }
                    , TaskCreationOptions.LongRunning)
                    .ContinueWith((task) => maxThread.Release()
                    );
                }
            }
            static void Main(string[] args)
            {
                Program p = new Program();
                p.MainAsync();
            }
    }

    you can see in task4() method takes more than 5 second as there is thread.sleep(5000); and so the next batches are executed. 

    Please help.

    Thanks

    Vijay

    Monday, July 2, 2018 7:40 AM

Answers

  • User-2017229834 posted

    Hi Everyone,

    I have found the answer.  I wanted this to this way.

       public void MainAsync()
            {
                var batches = new[] { 1, 2, 3, 4 };
              
                for (int i = 1; i <= batches.Length; i++)
                {
                    var taskList = new List<Task>();
                    Task t1 = task1(i);
                    Task t2 = task2(i);
                    Task t3 = task3(i);
                    Task t4 = task4(i);
                    var tasks = new[] { t1, t2, t3, t4 };
                    Task.WaitAll(tasks);
                }
            }

    Thanks All

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 2, 2018 12:07 PM

All replies

  • User475983607 posted

    I have created four async method, task1(), task2(), task3() and task4(). And one AsyncMain() Method to call these task. I want to call these method in batch - wise. like second batch will run only after the first batch completed. My code is here

    The problem you are trying to solve is not clear.  Simply remove async from the methods if you do not want async methods.

    Monday, July 2, 2018 10:36 AM
  • User-2017229834 posted

    Hi mgebhard,

    Thank you for your replay.

    I want to execute task1(),  task2(), task3() and task4() asynchronously in the loop. but loop will be pause till the first loop's tasks completed.

    I am changing MainAsync() Method. 

      public void MainAsync()
            {
                var batches = new[] { 1, 2, 3, 4 };
                for (int i = 1; i <= batches.Length; i++)
                {
                    var batch = Task.Run(() =>
                     {
                         Task.Run(() => task1(i));
                         Task.Run(() => task2(i));
                         Task.Run(() => task3(i));
                         Task.Run(() => task4(i));
                     });
                }
            }

    I want batch to be complete first and then it go for the next loop.

    Monday, July 2, 2018 10:59 AM
  • User475983607 posted

    Use the await keyword.

    await task1(i);
    await task2(i);
    await task3(i);
    await task4(i);

    Monday, July 2, 2018 11:40 AM
  • User-2017229834 posted

    Hi Everyone,

    I have found the answer.  I wanted this to this way.

       public void MainAsync()
            {
                var batches = new[] { 1, 2, 3, 4 };
              
                for (int i = 1; i <= batches.Length; i++)
                {
                    var taskList = new List<Task>();
                    Task t1 = task1(i);
                    Task t2 = task2(i);
                    Task t3 = task3(i);
                    Task t4 = task4(i);
                    var tasks = new[] { t1, t2, t3, t4 };
                    Task.WaitAll(tasks);
                }
            }

    Thanks All

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 2, 2018 12:07 PM