using System;
using System.Threading;
public class TestMain
{
private static ManualResetEvent ent = new ManualResetEvent(false);
public static void Main()
{
Boy sender = new Boy(ent);
Thread th = new Thread(new ThreadStart(sender.SendFlower));
th.Start();
ent.WaitOne();
Console.WriteLine("收到了吧,花是我送嘀:)");
Console.ReadLine();
}
}
public class Boy
{
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("/r/n花已经送到MM手中了,boss");
ent.Set();
}
}
本文介绍了一个简单的C#示例程序,演示如何使用ManualResetEvent来同步主线程与子线程间的操作。通过一个送花的场景,展示了如何等待一个线程完成其任务后再继续执行主线程的后续操作。
255

被折叠的 条评论
为什么被折叠?



