스레드로 작업하려면 코드에 다음 네임스페이스를 추가하세요 -
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!