Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 89 - ParameterizedThreadStart delegate

Suggested Videos 
Part 86 - Multithreading in C#
Part 87 - Advantages and disadvantages of multithreading
Part 88 - ThreadStart delegate



In this video we will discuss the purpose of ParameterizedThreadStart delegate. This is continuation to Part 88. Please watch Part 88 before proceeding. We will be working with the same example we worked with in Part 88.



Use ParameterizedThreadStart delegate to pass data to the thread function. Here is an example that shows the usage of ParameterizedThreadStart delegate.
using System;
using System.Threading;

namespace ThreadStartDelegateExample
{
    class Program
    {
        public static void Main()
        {
            Console.WriteLine("Please enter the target number");
            object target = Console.ReadLine();

            // Create an instance ParameterizedThreadStart delegate
            ParameterizedThreadStart parameterizedThreadStart =
                new ParameterizedThreadStart(Number.PrintNumbers);
            Thread T1 = new Thread(parameterizedThreadStart);
            // Pass the traget number to the start function, which
            // will then be passed automatically to PrintNumbers() function
            T1.Start(target);
        }
    }

    class Number
    {
        public static void PrintNumbers(object target)
        {
            int number = 0;
            if (int.TryParse(target.ToString(), out number))
            {
                for (int i = 1; i <= number; i++)
                {
                    Console.WriteLine(i);
                }
            }
        }
    }
}

The code in the Main() function can also be written as shown below.
public static void Main()
{
    Console.WriteLine("Please enter the target number");
    object target = Console.ReadLine();

    Thread T1 = new Thread(Number.PrintNumbers);
    T1.Start(target);
}

Here we are not explicitly creating an instance of ParameterizedThreadStart delegate. Then how is it working?
It's working because, the compiler implicitly converts new Thread(Number.PrintNumbers)  to new Thread(new ParameterizedThreadStart(Number.PrintNumbers)).

When to use ParameterizedThreadStart over ThreadStart delegate?
Use ParameterizedThreadStart delegate if you have some data to pass to the Thread function, otherwise just use ThreadStart delegate.

Please note: Using ParameterizedThreadStart delegate and Thread.Start(Object) method to pass data to the Thread function is not type safe as they operate on object datatype and any type of data can be passed. If you try to change the data type of the target parameter of PrintNumbers() function from object to int, a compiler error will be raised as the signature of PrintNumbers() function does not match with the signature of ParameterizedThreadStart delegate.

Next Video: Passing data to the Thread function without loosing the type safety.

1 comment:

  1. I have been watching many of your videos on you tube and they are so helpful. Also reading from your blog. Thank you so much for all these excellent tutorials. You are helping so much ! all is so clear and so well explained.

    ReplyDelete

It would be great if you can help share these free resources