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: 你好,世界!
热门推荐
5 薛之谦祝福语简短
10 分手快乐祝福语简短
11 写字生日祝福语简短独特
12 年底老板祝福语简短的
13 舞台上祝福语大全简短
14 新婚祝福语简短句子
15 孩子满月随礼简短祝福语
16 同事们生日祝福语简短
17 下雪路滑祝福语简短
18 升学祝福语简短的个性