Simplest way to run three methods in parallel in C#

前端 未结 5 1338
一向
一向 2020-12-04 14:01

I have three methods that I call to do some number crunching that are as follows

results.LeftFront.CalcAi();  
results.RightFront.CalcAi();  
results.RearSus         


        
相关标签:
5条回答
  • 2020-12-04 14:32

    In .NET 4, Microsoft introduced the Task Parallel Library which was designed to handle this kind of problem, see Parallel Programming in the .NET Framework.

    0 讨论(0)
  • 2020-12-04 14:35
    var task1 = SomeLongRunningTask();
    var task2 = SomeOtherLongRunningTask();
    
    await Task.WhenAll(task1, task2);
    

    The benefit of this over Task.WaitAll is that this will release the thread and await the completion of the two tasks.

    0 讨论(0)
  • 2020-12-04 14:39

    See the TPL documentation. They list this sample:

    Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());
    

    So in your case this should just work:

    Parallel.Invoke(
        () => results.LeftFront.CalcAi(),
        () => results.RightFront.CalcAi(),
        () => results.RearSuspension.CalcAi(geom, 
                                            vehDef.Geometry.LTa.TaStiffness, 
                                            vehDef.Geometry.RTa.TaStiffness));
    

    EDIT: The call returns after all actions have finished executing. Invoke() is does not guarantee that they will indeed run in parallel, nor does it guarantee the order in which the actions execute.

    0 讨论(0)
  • 2020-12-04 14:40

    You can do this with tasks too (nicer if you later need Cancellation or something like results)

    var task1 = Task.Factory.StartNew(() => results.LeftFront.CalcAi());
    var task2 = Task.Factory.StartNew(() => results.RightFront.CalcAi());
    var task3 = Task.Factory.StartNew(() =>results.RearSuspension.CalcAi(geom, 
                                  vehDef.Geometry.LTa.TaStiffness, 
                                  vehDef.Geometry.RTa.TaStiffness));
    
    Task.WaitAll(task1, task2, task3);
    
    0 讨论(0)
  • 2020-12-04 14:44

    To run parallel methods which are independent of each other ThreadPool.QueueUserWorkItem can also be used. Here is the sample method-

    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);
    }
    

    More detail about this function can be found here:
    http://newapputil.blogspot.in/2016/03/running-parallel-tasks-using.html

    0 讨论(0)
提交回复
热议问题