Mega Code Archive

 
Categories / C# / Thread
 

ThreadPool QueueUserWorkItem

using System; using System.Threading; class WinterLocked {     public ManualResetEvent a = new ManualResetEvent(false);     private int i = 5;     public void Run(object s) {         Interlocked.Increment(ref i);         Console.WriteLine("{0} {1}",                           Thread.CurrentThread.GetHashCode(), i);     } } public class MainApp {     public static void Main() {         ManualResetEvent mR = new ManualResetEvent(false);         WinterLocked wL = new WinterLocked();         for (int i = 1; i <= 10; i++) {             ThreadPool.QueueUserWorkItem(new WaitCallback(wL.Run), 1);         }         mR.WaitOne(10000, true);     } }