Created
August 31, 2023 18:56
-
-
Save admir-live/a6429d64de4f564fc9ce643f4ef6771c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
public class ThreadPoolExample | |
{ | |
public static void Main() | |
{ | |
// Queue a task. | |
ThreadPool.QueueUserWorkItem(DoWork, "Task 1"); | |
// Queue another task. | |
ThreadPool.QueueUserWorkItem(DoWork, "Task 2"); | |
// Wait for user input before exiting. | |
Console.ReadLine(); | |
} | |
// This method will be executed by a thread from the thread pool. | |
public static void DoWork(object state) | |
{ | |
Console.WriteLine($"Executing {state} on thread {Thread.CurrentThread.ManagedThreadId}"); | |
Thread.Sleep(2000); // Simulate some work. | |
Console.WriteLine($"{state} completed."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment