Threading

Thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.
So far we wrote the programs where a single thread runs as a single process which is the running instance of the application. However, this way the application can perform one job at a time. To make it execute more than one task at a time, it could be divided into smaller threads.

Example............
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace threads
{
    class Program
    {
        static void MethodX()
        {
            for (int i = 1; i <= 50; i++)
            {
                Console.WriteLine("X={0}",i);
            }
        }
        static void MethodY()
        {
            
            for (int i = 1; i <= 50; i++)
            {
               // Thread.Sleep(1000);
                Console.WriteLine("Y={0}",i);
            }
        }
        static void Main(string[] args)

        {
            Thread T1 = new Thread(new ThreadStart(MethodX));
            Thread T2 = new Thread(new ThreadStart(MethodY));
            T1.Start();
            T2.Start();
            for (int i = 1; i <= 10; i++)
                Console.WriteLine("delay");
            T1.Suspend();
            
            T1.Resume();
            T1.Abort();
            Console.ReadKey();
        }
    }


}

Comments