none
Parallel task execution at different time RRS feed

  • Question

  • Hello,

    I have console app which performs a couple of tasks in parallel. In my case the important part is that i want the tasks to complete at the same time. I know how long each task is going to take. So the idea is to delay somehow every task in the Parallel.ForEach with custom time delay per task so they all finish at the same time and in the end all the tasks will complete approximately at the same time as the one which takes most time.

    Example:

    interface IService
    {
        void DoWork();
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var services = new List<IService>();
            services.Add(new ServiceA());//Takes 500ms to DoWork()
            services.Add(new ServiceB());//Takes 1000ms to DoWork()
            services.Add(new ServiceC());//Takes 5000ms to DoWork()
    
            Parallel.ForEach(services, z =>
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                z.DoWork();
                Console.WriteLine($"Ready for {sw.Elapsed}");
            });
        }
    }

    Output:

    Ready for 00:00:00.5006837
    Ready for 00:00:01.0002284
    Ready for 00:00:05.0010202
    Press any key to continue . . .

    How to modify the code so the output will look like:

    Ready for 00:00:05.5006837
    Ready for 00:00:05.0002284
    Ready for 00:00:05.0010202
    Press any key to continue . . .

    I guess the most obvious solution would be to distinguish which Service is z in the loop and add custom Thread.Sleep before calling z.DoWork(), but I am looking for smarter solution.



    • Edited by ivanttyy Monday, November 13, 2017 3:12 PM wrong example
    Monday, November 13, 2017 2:23 PM

All replies


  • I think you're thinking it backwards. Why not move startup code of all three service to another threadso they can return immediately?
    Tuesday, November 14, 2017 1:17 AM
    Answerer
  • Hi,

    Thank you for posting here.

    For your question, I do not know how to defined the IService. I defined like the code below to test your code.

     interface IService
        {
            void DoWork();
        }
        class ServiceA : IService
        {
            void IService.DoWork()
            {
                //Console.WriteLine("ServiceA");
                Thread.Sleep(500);
            }
        }
        class ServiceB : IService
        {
            void IService.DoWork()
            {
                //Console.WriteLine("ServiceB");
                Thread.Sleep(1000);
            }
        }
        class ServiceC : IService
        {
            void IService.DoWork()
            {
                //Console.WriteLine("ServiceC");
                Thread.Sleep(5000);
            }
        }
        class Program
        {
    
            static void Main(string[] args)
            {
                var services = new List<IService>();
                services.Add(new ServiceA());//Takes 500ms to DoWork()
                services.Add(new ServiceB());//Takes 1000ms to DoWork()
                services.Add(new ServiceC());//Takes 5000ms to DoWork()
    
                Parallel.ForEach(services, z =>
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    z.DoWork();
                    Console.WriteLine($"Ready for {sw.Elapsed}");
                });
                Console.ReadKey();
    
            }

    The Parallel.Foreach Method is similar to the foreach statement, but traversal asynchronously, there is no definite traversal order. The three will execute asynchronously and output the result when it finished without any orders. It defined on the sleep time I defined by myself in the method.

    If you want to return at the same time, you could run it with synchronization. To run three tasks and then waiting for it to finish, after that output something.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, November 15, 2017 4:39 PM
    Moderator