这是线程之间交互的一个类,这里着重体现了初始化参数的选择。
namespace BaseTest
{
[TestClass]
public class ManualResetEventTest
{
public static string Test(bool init)
{
ManualResetEvent manual = new ManualResetEvent(init);
BlockingCollection<string> result = new BlockingCollection<string>();
Thread th = new Thread(() =>
{
manual.WaitOne();
result.Add("子线程");
});
th.Start();
Thread.Sleep(5);
result.Add("主线程");
manual.Set();
Thread.Sleep(5);
return string.Join("", result);
}
[TestMethod]
public void TestManualResetEvent_InitFalse()
{
string actual = Test(false);
Assert.AreEqual("主线程子线程", actual);
}
[TestMethod]
public void TestManualResetEvent_InitTrue()
{
string actual = Test(true);
Assert.AreEqual("子线程主线程", actual);
}
}
}