// Define a method to perform an operation on a thread pool thread void DoWork(object state) { // Perform some long running task } // Schedule the task on a thread pool thread ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork));
// Define a list of tasks to perform in parallel ListIn this example, a list of WorkItem objects is defined, and each object has a DoWork method that performs some task. The WaitCallback delegate is used to specify the DoWork method to be executed on a thread pool thread. The WaitCallback delegate is a part of the .NET Framework library in the System.Threading namespace.tasks = new List { new WorkItem(1), new WorkItem(2), new WorkItem(3), new WorkItem(4), new WorkItem(5) }; // Execute the tasks in parallel using thread pool threads foreach (var task in tasks) { ThreadPool.QueueUserWorkItem(new WaitCallback(task.DoWork)); }