1 using System;
2 using System.Threading;
3 
4 public class Test {
5 
6 	static int csum = 0;
7 
test_callback(object state)8 	public static void test_callback (object state) {
9 		int workerThreads;
10 		int completionPortThreads;
11 		ThreadPool.GetAvailableThreads (out workerThreads, out completionPortThreads);
12 		Console.WriteLine("test_casllback:" + state + "ATH: " + workerThreads);
13 		Thread.Sleep (10);
14 
15 		Interlocked.Increment (ref csum);
16 	}
17 
Main()18 	public static int Main () {
19 		int workerThreads;
20 		int completionPortThreads;
21 		int runs = 10;
22 
23 		ThreadPool.GetMaxThreads (out workerThreads, out completionPortThreads);
24 		Console.WriteLine ("workerThreads: {0} completionPortThreads: {1}", workerThreads, completionPortThreads);
25 
26 		ThreadPool.GetAvailableThreads (out workerThreads, out completionPortThreads);
27 		Console.WriteLine ("workerThreads: {0} completionPortThreads: {1}", workerThreads, completionPortThreads);
28 
29 		for (int i = 0; i < runs; i++) {
30 			ThreadPool.QueueUserWorkItem (new WaitCallback (test_callback), "TEST1 " + i);
31 			ThreadPool.QueueUserWorkItem (new WaitCallback (test_callback), "TEST2 " + i);
32 			ThreadPool.QueueUserWorkItem (new WaitCallback (test_callback), "TEST3 " + i);
33 			ThreadPool.QueueUserWorkItem (new WaitCallback (test_callback), "TEST4 " + i);
34 			ThreadPool.QueueUserWorkItem (new WaitCallback (test_callback), "TEST5 " + i);
35 
36 			do {
37 				ThreadPool.GetAvailableThreads (out workerThreads, out completionPortThreads);
38 				if (workerThreads == 0)
39 					Thread.Sleep (100);
40 			} while (workerThreads == 0);
41 
42 
43 			ThreadPool.GetAvailableThreads (out workerThreads, out completionPortThreads);
44 			Console.WriteLine ("workerThreads: {0} completionPortThreads: {1}", workerThreads, completionPortThreads);
45 		}
46 
47 		while (csum < (runs * 5)) {
48 			Thread.Sleep (100);
49 
50 		}
51 
52 		Console.WriteLine ("CSUM: " + csum);
53 
54 		if (csum != (runs * 5))
55 			return 1;
56 
57 		return 0;
58 	}
59 }
60 
61