private void BtnButton_Click(object sender, RoutedEventArgs e) { MessageBox.Show("hello Word"); } private void ModifyUI() { // 模擬一些工作正在進行 Thread.Sleep ...
private void BtnButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("hello Word");
}
private void ModifyUI()
{
// 模擬一些工作正在進行
Thread.Sleep(TimeSpan.FromSeconds(2));
//lblHello.Content = "歡迎你光臨WPF的世界,Dispatcher";
this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
{
lblHello.Content = "歡迎你光臨WPF的世界,Dispatche 同步方法 !!"+DateTime.Now.ToString();
MessageBox.Show("你好我是多線程同步方法");
});
}
private void btnThd_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(ModifyUI);
thread.Start();
}
/// <summary>
/// 非同步方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAppBeginInvoke_Click(object sender, RoutedEventArgs e)
{
new Thread(() =>
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action(() =>
{
Thread.Sleep(TimeSpan.FromSeconds(2));
this.lblHello.Content = "歡迎你光臨WPF的世界,Dispatche 非同步方法!!" + DateTime.Now.ToString();
MessageBox.Show("你好我是多線程非同步方法");
}));
}).Start();
}