C# – Multi Threading

Multi-threading is a process of executing two or more threads (programs) simultaneously to utilize the processor maximum.

Multi-tasking:

  • We can implement multi-tasking in two ways.
    • Program(process) based
    • Sub program (thread) based.

Single Threaded application:

  • The following example shows the execution process of program without threading.
  • In the Program, execution starts from default thread called main().
  • Execution become sequential as we are not created any custom threads.
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 value : ” + i);
                        }
            }
}
 
public class Second
{
            public static void print50to1()
            {
                        for (int j = 50; j >= 1; j–)
                        {
                                    Console.WriteLine(“j value : ” + j);
                        }
            }
}
Scroll to Top