110,533
社区成员
发帖
与我相关
我的任务
分享
namespace threadResume
{
public partial class Form1 : Form
{
private static AutoResetEvent event_1 = new AutoResetEvent(false);
Thread t = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(button1.Text=="开始")
{
if (t == null)
{
t = new Thread(ThreadProc);
t.Start();
}
event_1.Set();
button1.Text = "暂停";
}
else
{
event_1.Reset();
button1.Text = "开始";
}
}
public void ThreadProc()
{
event_1.WaitOne();
while (true)
{
Console.WriteLine("开始线程");
Thread.Sleep(3000);
}
}
}
}
Thread.Sleep(3000);
这类代码搞产品的人一看就知道是荒唐的逻辑:当你不知道要等待多久的时候,凭什么死机卡顿3秒钟?实际上不懂事件驱动的人,才会用“线程+死循环轮询+死机休眠”来代替事件概念。
异步交互操作编程是基本的事件驱动的知识,不了解事件驱动设计概念那么你起码有一般的软件设计知识没有学过(比如说你用过了那么多控件、模式,却从来没有理解事件委托的回调机制),不是什么乱用线程概念能模拟得好的。