none
How to store the return values when calling multiple function in parallel RRS feed

  • Question

  • public static void ExecuteParallel (params Action[] tasks)
            {
                // Initialize the reset events to keep track of completed threads
                ManualResetEvent [] resetEvents = new ManualResetEvent [tasks. Length];
    
                // Launch each method in it's own thread
                for (int i = 0; i < tasks . Length; i ++)
                {
                    resetEvents [i ] = new ManualResetEvent (false );
                    ThreadPool .QueueUserWorkItem ( new WaitCallback ((object index) =>
                    {
                        int taskIndex = ( int) index ;
    
                        // Execute the method
                        tasks [taskIndex ]();
    
                        // Tell the calling thread that we're done
                        resetEvents [taskIndex ]. Set();
                    }), i );
                }
    
                // Wait for all threads to execute
                WaitHandle .WaitAll ( resetEvents);
            }
      
    
                   //Sample use
    
              ExecuteParallel (() =>
              {
                   CalculateArea (),
                   CalculateDistance (),
                   CalculateTime ()
              });

    if all these functions CalculateArea (),  CalculateDistance (), CalculateTime () return some data then how to store them?

    if any exception occur in any function when they execute at parallel....what will happen ? other function will not execute ?

    again tell me how to know all the function running parallel completed?


    • Edited by Sudip_inn Saturday, September 7, 2019 7:58 PM
    Saturday, September 7, 2019 7:43 PM

Answers

  • Hi Sudip_inn,

    Thank you for posting here.

    You could use the following code to get the returned value when calling function in parallel.

     static void Main(string[] args)
            {
    
                DateTime d1 = new DateTime();
                double distance=0;
                int area=0;
                ExecuteParallel(() => d1 = CalculateTime(), () => distance = CalculateDistance(), () => area = CalculateArea());
                Console.WriteLine(d1);
                Console.WriteLine(distance);
                Console.WriteLine(area);
                Console.ReadKey();
    
            }
    
            private static DateTime CalculateTime()
            {
                return DateTime.Now;
            }
    
            private static double CalculateDistance()
            {
                return 18.02;
            }
    
            private static int CalculateArea()
            {
                return 1500;
            }

    Result:

    Best Regards,

    Jack


    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.

    • Marked as answer by Sudip_inn Monday, September 9, 2019 8:40 AM
    Monday, September 9, 2019 6:56 AM
    Moderator