Languages
[Edit]
EN

C#/.NET - create thread with parameters

4 points
Created by:
Kate_C
2857

To run new thread with parameters in C#/.NET Thread class and ParameterizedThreadStart delegate can be used.

Thread with parameters example

using System;
using System.Threading;

namespace Test
{
	class Program
	{
		static void Main(string[] args)
		{
			Thread thread = new Thread((object parameters) =>
			//Thread thread = new Thread(new ParameterizedThreadStart((object parameters) =>
			{
				string text = (string)parameters;
				//MyType variable = (MyType)parameters;

				Console.WriteLine(text);
			}));

			thread.Start("My parameter...");

			//MyType variable = new MyType();
			//thread.Start(variable);
		}
	}
}

Output:

My parameter...

References

  1. Thread Class - Microsoft Docs
  2. ParameterizedThreadStart Delegate - Microsoft Docs
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.