在别人的基础上修改成ManualResetEvent, 以取代suspend, resume.
public class Boy
{
private ManualResetEvent ent;
public Boy(ManualResetEvent e)
{
ent = e;
}
public void SendFlower()
{
Console.WriteLine("正在送花的途中");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(200);
Console.Write("..");
}
Console.WriteLine("花已经送到");
ent.Set();
}
}
class Program
{
private static ManualResetEvent ent = new ManualResetEvent(false);
static void Main(string[] args)
{
Boy sender = new Boy(ent);
for (int i = 0; i < 2; i++)
{
Thread th = new Thread(new ThreadStart(sender.SendFlower));
th.Start();
//Console.WriteLine("-before wait?-" + ent.WaitOne(0)); //false
ent.WaitOne();
//Console.WriteLine("-before reset?-" + ent.WaitOne(0)); //true
ent.Reset();
//Console.WriteLine("-after reset?-" + ent.WaitOne(0)); //false
Console.WriteLine("收到了吧,花是我送嘀:)/r/n/r/n");
}
Console.ReadLine();
}
}