Languages
[Edit]
EN

C#/.NET - create thread with parameters

4 points
Created by:
Kate_C
19800

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
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join