Để tạo một chuỗi, tôi đã tạo một hàm -
public void myThread() {
for (int i = 0; i < 3; i++) {
Console.WriteLine("My Thread");
}
} Hàm trên được gọi để tạo một luồng và một đại biểu ThreadStart mới được tạo -
Demo d = new Demo(); Thread thread = new Thread(new ThreadStart(d.myThread));
Ví dụ
Tạo một chuỗi đơn giản bằng đoạn mã sau.
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();
}
} Đầu ra
My Thread My Thread My Thread