C# Threading

C# – Multi Threading

Multi-threading is a process of executing two or more threads (programs) simultaneously to utilize the processor maximum. Multi-tasking: Single Threaded application: using System;namespace ThreadingApp{            internal class Program            {                        static void Main(string[] args)                        {                                    First.print1to50();                                    Second.print50to1();                        }            }} public class First{            public static void print1to50()            {                        for (int i = 1; i <= 50; i++)                        {                                    Console.WriteLine(“i …

C# – Multi Threading Read More »

C# – sleep() method in Thread

sleep(): using System;using System.Threading;public class Program{            static void Main(string[] args)            {                        First.print1to10();                        Second.print10to1();            }}public class First{            public static void print1to10()            {                        for (int i=1; i<=10; i++)                        {                                    Console.WriteLine(“i value : ” + i);                                    Thread.Sleep(1000);                        }            }}class Second{            public static void print10to1()            {                        for(int j=10; j>=1; j–)                        {                                    Console.WriteLine(“j value : ” + j);                                    Thread.Sleep(1000);                        }            …

C# – sleep() method in Thread Read More »

C# – Define Custom Thread

Define Custom thread: Thread Life Cycle: Every thread has undergone different states in its Life. using System;using System.Threading;public class Program{            static void Main(string[] args)            {                        Thread t1 = new Thread(new ThreadStart(First.print1to10));                        Thread t2 = new Thread(new ThreadStart(Second.print10to1));                        t1.Start();                        t2.Start();            }}public class First{            public static void print1to10()            {                        for (int i=1; i<=10; i++)                        {                                    Console.WriteLine(“i value …

C# – Define Custom Thread Read More »

C# – join() method

join(): Example: Consider First thread is calculating sum of first N numbers and second thread has to display the result. The Second thread has to wait until First thread completes calculation. using System;using System.Threading;public class Program{            public static int n;            static void Main(string[] args)            {                        Console.WriteLine(“Enter n value : “);                        Program.n = Convert.ToInt32(Console.ReadLine());                        Thread t …

C# – join() method Read More »

C# – Abort() method

Abort() method: using System;using System.Threading;public class MyThread{            public void Thread1()            {                        for (int i=0; i<10; i++)                        {                                    Console.WriteLine(i);                                    Thread.Sleep(200);                        }            }}public class Program{            public static void Main()            {                        Console.WriteLine(“Start of Main”);                        MyThread mt = new MyThread();                        Thread t1 = new Thread(new ThreadStart(mt.Thread1));                        Thread t2 = new Thread(new ThreadStart(mt.Thread1));                        t1.Start();                        t2.Start();                        try                        {                                    t1.Abort();                                    t2.Abort();                        }                        …

C# – Abort() method Read More »

C# – Thread Synchronization

Thread Synchronization: using System;using System.Threading;public class LockDisplay{            public void DisplayNum()            {                        lock (this)                        {                                    for (int i = 1; i <= 5; i++)                                    {                                                Thread.Sleep(100);                                                Console.WriteLine(“i = {0}”, i);                                    }                        }            }}class Example{            public static void Main(string[] args)            {                        LockDisplay obj = new LockDisplay();                        Thread t1 = new Thread(new ThreadStart(obj.DisplayNum));                        Thread t2 = new Thread(new …

C# – Thread Synchronization Read More »

Scroll to Top