What is TPL in C#? Example.

Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces. 

The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications.
 
Example

class Program
    {
        static void Main(string[] args)
        {
            //Action delegate 
            Task task1 = new Task(new Action(HelloConsole));

            //anonymous function 
            Task task2 = new Task(delegate
            {
                HelloConsole();
            });

            //lambda expression 
            Task task3 = new Task(() => HelloConsole());

            task1.Start();
            task2.Start();
            task3.Start();

            Console.WriteLine("Main method complete. Press any key to finish.");
            Console.ReadKey(); 
        }
        static void HelloConsole()
        {
            Console.WriteLine("Hello Task");
        } 
    }
Share:

No comments:

Post a Comment

Wednesday, 4 September 2019

What is TPL in C#? Example.

Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces. 

The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications.
 
Example

class Program
    {
        static void Main(string[] args)
        {
            //Action delegate 
            Task task1 = new Task(new Action(HelloConsole));

            //anonymous function 
            Task task2 = new Task(delegate
            {
                HelloConsole();
            });

            //lambda expression 
            Task task3 = new Task(() => HelloConsole());

            task1.Start();
            task2.Start();
            task3.Start();

            Console.WriteLine("Main method complete. Press any key to finish.");
            Console.ReadKey(); 
        }
        static void HelloConsole()
        {
            Console.WriteLine("Hello Task");
        } 
    }

No comments:

Post a Comment

Popular

Total Pageviews

Archive