开门见山,下面的例子中通过调用ThreadPool.QueueUserWorkItem(WaitCallback callBack, object state)的方式实现异步调用:
代码语言:js复制 1: class Program
2: {
3: static void Main(string[] args)
4: {
5: List<Action> actions = new List<Action>();
6: actions.Add(() => Console.WriteLine("A1"));
7: actions.Add(() => Console.WriteLine("A2"));
8: actions.Add(() => Console.WriteLine("A3"));
9: actions.Add(() => Console.WriteLine("A4"));
10:
11: foreach (var action in actions)
12: {
13: ThreadPool.QueueUserWorkItem(state => action(), null);
14: }
15:
16: Console.Read();
17: }
18: }
但是出现错误的输出结果:
解决的方案就是在每次For循环中,调用Thread.Sleep休眠当前线程,哪怕是1ms:
代码语言:js复制 1: class Program
2: {
3: static void Main(string[] args)
4: {
5: List<Action> actions = new List<Action>();
6: actions.Add(() => Console.WriteLine("A1"));
7: actions.Add(() => Console.WriteLine("A2"));
8: actions.Add(() => Console.WriteLine("A3"));
9: actions.Add(() => Console.WriteLine("A4"));
10:
11: foreach (var action in actions)
12: {
13: ThreadPool.QueueUserWorkItem(state => action(), null);
14:
15: Thread.Sleep(1);
16: }
17:
18: Console.Read();
19: }
20: }
21:
这次能够输出正确的结果:
我们也看到很多人确实是这么做的。但是如果真是必须这样的话,这样的编程方式很难让我接受,不知道大家有何高见。
在老赵的提示下,醒悟过来:由于被置于ThreadPool中的操作时异步的,还没有来的执行的时候,action已经被for循环改变,永远是同一个action对象! 呵呵,脑袋有时候有点转不过弯!
所以正确的写法是:
代码语言:js复制 1: foreach (var action in actions)
2: {
3: var a = action;
4: ThreadPool.QueueUserWorkItem(state => a(), null);
5: }