थ्रेड्स के साथ काम करने के लिए, अपने कोड में निम्नलिखित नेमस्पेस जोड़ें -
using System.Threading;
सबसे पहले, आपको C# में एक नया थ्रेड बनाना होगा -
Thread thread = new Thread(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!