How to Pass data to the Thread Function in a Type Safe Manner

How to Pass data to the Thread Function in a Type-Safe Manner

In this article, I am going to discuss how to pass data to the thread function in a type-safe manner. Please read our previous article before proceeding to this article where we discussed the Thread class constructors in C# with some examples. As part of this article, we are going to discuss the following pointers.

  1. How to Pass data to the Thread function in C#?
  2. How to make the thread function type-safe in C#

We have written the following program in our previous article.

using System.Threading;
using System;
namespace ThreadingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Program obj = new Program();
            ParameterizedThreadStart PTSD = new ParameterizedThreadStart(obj.DisplayNumbers);
            Thread t1 = new Thread(PTSD);
           
            t1.Start("Hi"); 
            Console.Read();
        }

       public void DisplayNumbers(object Max)
        {
            int Number = Convert.ToInt32(Max);
            for (int i = 1; i <= Number; i++)
            {
                Console.WriteLine("Method1 :" + i); 
            }  
        }
    }
}

At the time of compilation, we will not get any compile-time error. But when we run the application, we will get the following runtime error.

How to Pass data to the Thread Function in a Type-Safe Manner

This is because the thread function is not type-safe as it operates on the object data type. Let see how to make the thread function type so that we can pass the data in a type-safe manner.

Making Thread Function Type-Safe:

When we are saying type-safe means we should not have to use the object data type. Here in our example, we need to use the data type as an integer. So at the time compilation, we pass any data other than integer then it should givens you a compile-time error. Let us see how to achieve this step by step.

Step1:

In order to pass the data in a type-safe manner to the Thread function, first, you need to encapsulate the thread function and the data it requires in a helper class. So, create a class file with the NumberHelper.cs and then copy and paste the following code in it.

using System;
namespace ThreadingDemo
{
    public class NumberHelper
    {
        int _Number;
        
        public NumberHelper(int Number)
        {
            _Number = Number;
        }
        
        public void DisplayNumbers()
        {
            for (int i = 1; i <= _Number; i++)
            {
                Console.WriteLine("value : " + i);
            }
        }
    }
}

As you can see we created the above Number Helper class with one private variable, one parameterized constructor and one method. The private variable _Number is going to hold the target number. The constructor takes one input parameter of integer type and then it assigns that value to the private variable. So, while we are creating the instance of NumberHelper class we need to supply the Number value. Finally, the DisplayNumbers function is used to display the values starting from 1 to the value that is present in the _Number variable.

Step2:

In the main method create an instance of NumberHelper class and to its constructor pass the integer value. Then create the ThreadStart delegate instance and pass the Display Number function. So, please modify the Main method as shown below.

using System.Threading;
using System;
namespace ThreadingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int Max = 10;
            NumberHelper obj = new NumberHelper(Max);
            
            Thread T1 = new Thread(new ThreadStart(obj.DisplayNumbers));
            
            T1.Start();
            Console.Read();
        }
    }
} 

Now run the application and it should display the output as expected as shown below.

Making the Thread Function Type-Safe in C#

In the next article, I am going to discuss how to retrieve the data from a thread function using a callback method in C#. Here, in this article, I try to explain how to pass data to the thread function in a type-safe manner. I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *