ThreadPool.QueueUserWorkItem

image_pdfimage_print

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); } } [/csharp]

This entry was posted in Thread. Bookmark the permalink.