Languages
[Edit]
EN

C#/.NET - create thread with parameters

4 points
Created by:
Marcino
720

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# - Thread Class

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join