Home  >  Article  >  Backend Development  >  C# program that passes parameters to threads

C# program that passes parameters to threads

WBOY
WBOYforward
2023-08-27 10:25:09863browse

将参数传递给线程的 C# 程序

To use threads, add the following namespace in your code -

using System.Threading;

First you need to create a new thread in C# -

Thread thread = new Thread(threadDemo);

Above, threadDemo is our thread function.

Now pass the parameters to the thread-

thread.Start(str);

The parameters set above are-

String str = "Hello World!";

Example

Let us see passing the parameters in C# Give the complete code to the thread.

Real-time demonstration p>

using System;
using System.Threading;
namespace Sample {
   class Demo {
      static void Main(string[] args) {
         String str = "Hello World!";
         // new thread
         Thread thread = new Thread(threadDemo);
         // passing parameter
         thread.Start(str);
      }
      static void threadDemo(object str) {
         Console.WriteLine("Value passed to the thread: "+str);
      }
   }
}

Output

Value passed to the thread: Hello World!

The above is the detailed content of C# program that passes parameters to threads. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete