111,037
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Threading;
[System.Runtime.Remoting.Contexts.Synchronization(true)] // 1
class My : ContextBoundObject
{
static void Main(string[] args)
{
My my = new My();
ThreadPool.QueueUserWorkItem(my.FuncA);
Thread.Sleep(50);
ThreadPool.QueueUserWorkItem(my.FuncA);
Thread.Sleep(50);
ThreadPool.QueueUserWorkItem(my.FuncB);
Thread.Sleep(50);
ThreadPool.QueueUserWorkItem(my.FuncA);
Console.ReadLine();
}
AutoResetEvent myEvent = new AutoResetEvent(false);
public void FuncA(object state)
{
Console.WriteLine("Start A");
System.Threading.Thread.Sleep(2000);
Console.WriteLine("End A");
}
public void FuncB(object state)
{
Console.WriteLine("Start B");
myEvent.WaitOne(10 * 1000, true); // 2
Console.WriteLine("End B");
}
}