Suraj Kumar

Suraj Kumar

  • NA
  • 9.9k
  • 2.2m

ThreadPool.QueueUserWorkItem is not working in C# Console

Jun 1 2019 2:27 AM
I am trying to use ThreadPool in C# Console application as shown below.
  1. static void Main(string[] args)  
  2. {  
  3.             for (var i = 1;  i<= 10; i++)  
  4.             {  
  5.                 ThreadPool.QueueUserWorkItem(n =>  
  6.                 {  
  7.                     Console.WriteLine($"Start operation {n}");  
  8.                     Thread.Sleep(1000); // Simulate long running operation  
  9.                     Console.WriteLine($"Operation {n} completed");  
  10.                 }, i);  
  11.             }  
  12.             Console.ReadKey();  
  13. }  
But it is showing the below error:
 
The type or namespace name "QueueUserWorkItem"  does not exist in the namespace "ThreadPool".
 
What I am doing wrong or which reference I need to add. I have already used the namespace system.Threading.
 
I am working on .Net framework version 4.6.

Answers (1)