C#程序将参数传递给线程
要使用线程,请在代码中添加以下命名空间-
using System.Threading;
首先,您需要在C#中创建一个新线程-
Thread thread =新线程(threadDemo);
上面,threadDemo是我们的线程函数。
现在将参数传递给线程-
thread.Start(str);
上面设置的参数是-
String str = "你好,世界!";
示例
让我们看完整的代码,以将参数传递给C#中的线程。
using System;
using System.Threading;
namespace Sample {
class Demo {
static void Main(string[] args) {
String str = "你好,世界!";
//新线程
Thread thread =新线程(threadDemo);
//传递参数
thread.Start(str);
}
static void threadDemo(object str) {
Console.WriteLine("Value passed to the thread: "+str);
}
}
}输出结果
Value passed to the thread: 你好,世界!
热门推荐
10 双十一祝福语大全简短
11 经典祝福语简短情侣句子
12 汤姆猫祝福语简短霸气
13 写字生日祝福语简短独特
14 给医生简短新年祝福语
15 贺卡文字同事祝福语简短
16 亲戚送行祝福语简短精辟
17 阳历新年祝福语大全 简短
18 送礼祝福语男性生日简短