एक थ्रेड बनाने के लिए, मैंने एक फंक्शन बनाया है -
public void myThread() {
for (int i = 0; i < 3; i++) {
Console.WriteLine("My Thread");
}
} उपरोक्त फ़ंक्शन को थ्रेड बनाने के लिए कहा जाता है और एक नया थ्रेडस्टार्ट प्रतिनिधि बनाया जाता है -
Demo d = new Demo(); Thread thread = new Thread(new ThreadStart(d.myThread));
उदाहरण
निम्न कोड का उपयोग करके एक साधारण थ्रेड बनाएं।
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
class Demo {
public void myThread() {
for (int i = 0; i < 3; i++) {
Console.WriteLine("My Thread");
}
}
}
class NewThread {
public static void Main() {
Demo d = new Demo();
Thread thread = new Thread(new ThreadStart(d.myThread));
thread.Start();
Console.Read();
}
} आउटपुट
My Thread My Thread My Thread