您好)请帮助我,我需要按下按钮让线程睡眠,然后按下按钮1继续此线程。我处理WPF,当我在button1_click事件中调用WaitOne()方法时,我的表单变得冻结,我无法点击任何按钮。这是我的代码示例:
AutoResetEvent objAuto = new AutoResetEvent(false);
private void Button_Click(object sender, RoutedEventArgs e)
{
if (thread != null)
{
objAuto.Set();
}
thread = new Thread(new ThreadStart(zoo.FeedAnimals));
thread.Start();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
objAuto.WaitOne();
}
感谢您提前
答案 0 :(得分:1)
冻结的原因是因为你在UI线程上调用WaitOne
。你可能想要一些东西
AutoResetEvent objAuto = new AutoResetEvent(false);
private void Button_Click(object sender, RoutedEventArgs e)
{
thread = new Thread(new ThreadStart(zoo.FeedAnimals));
thread.Start();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (thread != null)
{
objAuto.Set(); // this is in the main UI thread
}
}
public void FeedAnimals()
{
...
objAuto.WaitOne(); // this blocks your other thread
...
}