Book Image

Learning .NET High-performance Programming

By : Antonio Esposito
Book Image

Learning .NET High-performance Programming

By: Antonio Esposito

Overview of this book

Table of Contents (16 chapters)
Learning .NET High-performance Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Task parallelism with TPL


As mentioned earlier, task parallelism happens when dealing with parallel invocations of multiple methods/function. Within the .NET Framework, this can be obtained with the invocation of the Parallel.Invoke method, which needs to have as a parameter all parallelizable actions as a whole. Most techniques applicable here are also applicable in asynchronous programming with the Task or the TaskFactory class. So reading Chapter 4, Asynchronous Programming is mandatory to get the best of task parallelism.

The Parallel.Invoke method simply takes multiple remote methods to call procedures in a parallel way by accepting a System.Action array. Here is an example:

static void Main(string[] args)
{
    //short form with named methods
    Parallel.Invoke(Method1, Method2, Method3);

    //short form with anonymous methods
    Parallel.Invoke(
        () => { },
        () => { },
        () => { });
}

static void Method1() { }
static void Method2() { }
static void Method3...