In this tutorial article we will learn about thread pooling in c# using simple Example.
At the end, the sample will work as in following picture.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.Diagnostics; using System.Threading; namespace CSharpConsoleExamples { class Program { public static void Main() { // Queue the task. ThreadPool.QueueUserWorkItem(ThreadA); ThreadPool.QueueUserWorkItem(ThreadB); Console.WriteLine("Main thread does some work, then sleeps."); Thread.Sleep(2000); Console.WriteLine("Main thread exits."); Console.ReadLine(); } static void ThreadA(Object stateInfo) { for (int i = 0; i <= 3; i++) { Console.WriteLine("Task A is being executed"); Thread.Sleep(1000); } } static void ThreadB(Object stateInfo) { for (int i = 0; i <= 3; i++) { Console.WriteLine("Task B is being executed"); Thread.Sleep(1500); } } } } |
Next example:Thread Pool in C# Example With Parameters