Threadstart-and-parameterizedthreadstart


ThreadStart and ParemeterizedThreadStart

ThreadStart

A ThreadStart is a delegate defined in the System.Threading namespace that represents the method that executes on a Thread.

Threads are lines of executions within a process and allow us write concurrent programs.

To be able to do this we need to pass them the method that needs to be run in the background.

Normally we use the ThreadStart delegate to hold the reference to that method.

Typically, methods don't get passed around constructors or other methods. But delegates allow us to do this, so the ThreadStart delegate allows us pass methods to the Thread constructor.

The ThreadStart class is defined in the following manner:

public delegate void ThreadStart();

This is a top level object inside the System.Threading namespace, which is itself defined in the mscorlib.dll assembly.

Let's look at an example:

using System;
using System.Threading;

namespace MrThreadStart
{
    class Program
    {
        static void Main()
        {
            ThreadStart myThreadStart=showGalaxies;
            Thread myThread = new Thread(myThreadStart);
            myThread.Start();
            Console.ReadLine();

        }
        static void showGalaxies()
        {
            Console.WriteLine("Starting Galaxies Thread.............");
            string[] nebulas = { "IC 1101", "Andromeda", "Centaurus A","Milky Way", "Cartwheel","Sombrero","Black Eye Galaxy","Pinwheel" };
            foreach (var nebula in nebulas)
            {
                Console.WriteLine(nebula);
            }
        }
    }
}

Result

Starting Galaxies Thread.............
IC 1101
Andromeda
Centaurus A
Milky Way
Cartwheel
Sombrero
Black Eye Galaxy
Pinwheel

As you can see we first instantiate the ThreadStart delegate and assign it the method to hold.

You can do this in either of the following two ways:

  1. ThreadStart myThreadStart=new ThreadStart(showGalaxies);
  2. ThreadStart myThreadStart=showGalaxies;

The first version is the explicit way to create our delegate.

The important thing is to assign it the method that it will hold. Then instantiate the Thread class and pass the ThreadStart delegate instance to the constructor:

Thread myThread = new Thread(myThreadStart);

Then start the thread:

myThread.Start();

ParameterizedThreadStart

ParameterizedThreadStart is a delegate defined in the System.Threading namespace that represents the method that executes on a thread.

Unlike the ThreadStart delegate, it takes in a parameter of type Object. That Object contains the data for the thread procedure.

public delegate void ParameterizedThreadStart(object obj);

Let's look at an example.

using System;
using System.Threading;

namespace MrParameterizedThreadStart
{
    class Program
    {
        static readonly string[] planets = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
        static void Main()
        {
           // ParameterizedThreadStart myParameterizedThreadStart = new ParameterizedThreadStart(o => showGalaxies(planets));
            ParameterizedThreadStart myParameterizedThreadStart = o => showGalaxies(planets);
            Thread myThread = new Thread(myParameterizedThreadStart);
            myThread.Start();
            Console.ReadLine();

        }
        static void showGalaxies(string[] myPlanets)
        {
            Console.WriteLine("Starting Planets Thread.............");
            foreach (var planet in myPlanets)
            {
                Console.WriteLine(planet);
            }
        }
    }
}

Results

Starting Planets Thread.............
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

We are assigning a lambda expression to our ParameterizedThreadStart instance:

ParameterizedThreadStart myParameterizedThreadStart = o => showGalaxies(planets);

We can also explicitly instantiate the ParameterizedThreadStart:

ParameterizedThreadStart myParameterizedThreadStart = new ParameterizedThreadStart(o => showGalaxies(planets));

We need to make sure that the method whose address we are holding in our delegate takes in a parameter for us to use the ParameterizedThreadStart:

static void showGalaxies(string[] myPlanets){}

Then we will instantiate the Thread and pass it our ParameterizedThreadStart instance:

Thread myThread = new Thread(myParameterizedThreadStart);

Then start the thread:

 myThread.Start();

Best Regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post