Start a new Thread in C#

In this article, I will explain how to start a new thread by using Thread.Start method in .NET Framework.
  • 11838

Introduction

We can create a new thread by using newThreadStart delegate. This Delegate holds the reference to a method that will be executed by the new thread. Finally, we can call the Thread.Start method to run the method.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading; 

namespace ThreadStart

{

    class Program

    {

        static void Main(string[] args)

        {

            Thread th = new Thread(new ThreadStart(WriteY));

            th.Start();

            for (int i = 0; i <= 10; i++)

            {

                Console.WriteLine("Hello");

            }

            Console.ReadLine();

        }

        static void WriteY()

        {

            for (int i = 0; i <= 9; i++)

            {

                Console.Write("HI");

            }

        } 

    }

}

Output

Clipboard251.jpg

© 2013 dotNetheaven. All rights reserved.