Skip to content

Instantly share code, notes, and snippets.

@admir-live
Created August 31, 2023 18:56
Show Gist options
  • Save admir-live/a6429d64de4f564fc9ce643f4ef6771c to your computer and use it in GitHub Desktop.
Save admir-live/a6429d64de4f564fc9ce643f4ef6771c to your computer and use it in GitHub Desktop.
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