Tuesday, 26 November 2013

Creating and Starting Threads using C#

Code:


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

namespace Creating_and_StartingThread_usingCSharp
{
    class threadclass
    {
        public void First()
        {
            Console.WriteLine("First method of threadclass is running on T1 thread");
            Thread.Sleep(1000);
            Console.WriteLine("The second method called by T2 thread has ended");
        }
        public void Second()
        {
            Console.WriteLine("Second method of threadclass is running on T2 thread");
            Thread.Sleep(1000);
            Console.WriteLine("The second method called by T2 thread has ended");
        }

    public    static void Main(string[] args)
        {
            Console.WriteLine("Example of Threading");
        threadclass a =new threadclass();
        Thread T1=new Thread(new ThreadStart(a.First));
        T1.Start();
        Console.WriteLine("T1 thread started.");
        Thread T2 = new Thread(new ThreadStart(a.Second));
        T2.Start();
        Console.WriteLine("T2 thread started.");
        Console.ReadKey();

       
        }
    }
}

Output:

No comments:

Post a Comment