CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 120

120
Chương 4: Tiểu trình, tiến trình, và sự đồng bộ

Console.WriteLine(config.Message);

// Vào trạng thái chờ, dùng cho mục đích minh họa.

// Tránh đưa các tiểu trình của thread-pool

// vào trạng thái chờ trong các ứng dụng thực tế.

Thread.Sleep(1000);

}

}

}

public static void Main() {

// Tạo một đối tượng ủy nhiệm, cho phép chúng ta

// truyền phương thức DisplayMessage cho thread-pool.

WaitCallback workMethod =

new WaitCallback(ThreadPoolExample.DisplayMessage);

// Thực thi DisplayMessage bằng thread-pool (không có đối số).

ThreadPool.QueueUserWorkItem(workMethod);

// Thực thi DisplayMessage bằng thread-pool (truyền một

// đối tượng MessageInfo cho phương thức DisplayMessage).

MessageInfo info =

new MessageInfo(5, "A thread-pool example with arguments.");

ThreadPool.QueueUserWorkItem(workMethod, info);

// Nhấn Enter để kết thúc.

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}

}