C# 程序向线程传递参数
要使用线程,在代码中添加以下命名空间 -
using System.Threading;
首先,你需要在 C# 中创建一个新线程 -
Thread thread = new Thread(threadDemo);
上面,threadDemo 是我们的线程函数。
现在,将一个参数传递给线程 -
thread.Start(str);
上面设置的参数是 -
String str = "Hello World!";
示例
让我们来看看将一个参数传递给 C# 中线程的完整代码。
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);
}
}
}输出
Value passed to the thread: Hello World!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP