WPF 小小案列(同步异步)

 1 private void BtnButton_Click(object sender, RoutedEventArgs e)
 2 {
 3 MessageBox.Show("hello Word");
 4 }
 5 private void ModifyUI()
 6 {
 7 // 模拟一些工作正在进行
 8 Thread.Sleep(TimeSpan.FromSeconds(2));
 9 //lblHello.Content = "欢迎你光临WPF的世界,Dispatcher";
10 this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
11 {
12 lblHello.Content = "欢迎你光临WPF的世界,Dispatche 同步方法 !!"+DateTime.Now.ToString();
13 MessageBox.Show("你好我是多线程同步方法");
14 });
15 }
16 
17 private void btnThd_Click(object sender, RoutedEventArgs e)
18 {
19 Thread thread = new Thread(ModifyUI);
20 thread.Start();
21 }
22 /// 
23 /// 异步方法
24 /// 
25 /// 
26 /// 
27 private void btnAppBeginInvoke_Click(object sender, RoutedEventArgs e)
28 {
29 new Thread(() =>
30 {
31 Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
32 new Action(() =>
33 {
34 Thread.Sleep(TimeSpan.FromSeconds(2));
35 
36 this.lblHello.Content = "欢迎你光临WPF的世界,Dispatche 异步方法!!" + DateTime.Now.ToString();
37 MessageBox.Show("你好我是多线程异步方法");
38 }));
39 }).Start();
40 }

 

WPF 小小案列(同步异步)_第1张图片

WPF 小小案列(同步异步)_第2张图片

 

你可能感兴趣的:(WPF 小小案列(同步异步))