Creating thread with lambda expressions

Instead of defining a separate method for thread to execute, we can use lamda expression notation to execute the thread.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ThreadPrograms
{
    class Program
    {
        static void Main(string[] args)
        {

            var t1 = new Thread(() => 
            Console.WriteLine("Running in a thread, id: {0}", 
            Thread.CurrentThread.ManagedThreadId));

            t1.Start();

            Console.WriteLine("Running in a Main Thread, id: {0}",
            Thread.CurrentThread.ManagedThreadId);

        }

        
    }
}

 

 

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *