//将方法包装成适于线程调用的签名 static void DoSomethingWapper(object state) { ClosureClass closure = state as ClosureClass; var result = DoSomething(); if (closure != null) { closure.Result = result; } }
/// <summary> /// 异步 /// </summary> static void Demo01() { Thread writeThread = new Thread(new ThreadStart(WriteWapper)); Thread doSomethingThread = new Thread(new ParameterizedThreadStart(DoSomethingWapper)); ClosureClass closure = new ClosureClass(); writeThread.Start(); doSomethingThread.Start(closure);//闭包对象,用于变量穿越 writeThread.Join(); doSomethingThread.Join(); Console.WriteLine(closure.Result); }