Home > Backend Development > C#.Net Tutorial > C# program that passes parameters to threads

C# program that passes parameters to threads

WBOY
Release: 2023-08-27 10:25:09
forward
906 people have browsed it

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

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

using System.Threading;
Copy after login

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

Thread thread = new Thread(threadDemo);
Copy after login

Above, threadDemo is our thread function.

Now pass the parameters to the thread-

thread.Start(str);
Copy after login

The parameters set above are-

String str = "Hello World!";
Copy after login

Example

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

Real-time demonstration

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);
      }
   }
}
Copy after login

Output

Value passed to the thread: Hello World!
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template