使用WaitHandle

来源:互联网 发布:淘宝6.6.0 编辑:程序博客网 时间:2024/05/29 03:53

1.使用WaitHandle

  等待句柄应当是你进行多线程编程的必备装备。由于我们的主要兴趣点在于Silverlight多线程编程相关的内容,所以我们不想再深入探讨WaitHandle。但在此为你提供一个典型的例子,告诉你使用WaitHandle的基本方法。

  清单1:

  public partial class MainPage : UserControl

  {

  AutoResetEvent handle =
new AutoResetEvent(true);

  public MainPage()

  {

  InitializeComponent();

  
new Thread(() =>

  {

  
while (true)

  {

  handle.WaitOne();

  this.Dispatcher.BeginInvoke(() =>

  {

  this.TextBlock1.Text = DateTime.Now.ToString();

  });

  }

  }).Start();

  }

  
private void Button_Click(object sender, RoutedEventArgs e)

  {

  handle.Set();

  }

  }

 

 

 

原创粉丝点击