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