WPF 异步
Code
1 public partial class Window1 : Window
2 {
3 /**//// <summary>
4 /// 异步调用的委托
5 /// </summary>
6 /// <param name="name"></param>
7 /// <returns></returns>
8 public delegate string AsyncEventHandler(string name);
9 /**//// <summary>
10 /// 异步完成更新界面的委托
11 /// </summary>
12 /// <param name="value"></param>
13 public delegate void UpdateUI(string value);
14
15 public Window1()
16 {
17 InitializeComponent();
18 this.btn1.Click += new RoutedEventHandler(btn1_Click);
19 }
20
21 void btn1_Click(object sender, RoutedEventArgs e)
22 {
23 //实例化委托
24 AsyncEventHandler async = new AsyncEventHandler(GetValue);
25 //开始异步
26 IAsyncResult ia = async.BeginInvoke("Hello World!", new AsyncCallback(CallBack), async);
27 }
28
29 /**//// <summary>
30 /// 异步完成CallBack方法
31 /// </summary>
32 /// <param name="result"></param>
33 private void CallBack(IAsyncResult result)
34 {
35 //转换返回值为委托类型
36 AsyncEventHandler async = (AsyncEventHandler)result.AsyncState;
37 //获得异步委托的返回值
38 string value = async.EndInvoke(result);
39 //实例化更新界面的委托
40 UpdateUI method = new UpdateUI(Update);
41 //开始界面委托
42 this.Dispatcher.Invoke(method, value);
43 }
44
45
46 /**//// <summary>
47 /// 更新界面
48 /// </summary>
49 /// <param name="value"></param>
50 private void Update(string value)
51 {
52 this.lbl1.Content = value;
53 }
54
55 /**//// <summary>
56 /// 调用的方法
57 /// </summary>
58 /// <param name="value"></param>
59 /// <returns></returns>
60 private string GetValue(string value)
61 {
62 Thread.Sleep(10000);
63 return "say:" + value;
64 }
65 }
1 public partial class Window1 : Window
2 {
3 /**//// <summary>
4 /// 异步调用的委托
5 /// </summary>
6 /// <param name="name"></param>
7 /// <returns></returns>
8 public delegate string AsyncEventHandler(string name);
9 /**//// <summary>
10 /// 异步完成更新界面的委托
11 /// </summary>
12 /// <param name="value"></param>
13 public delegate void UpdateUI(string value);
14
15 public Window1()
16 {
17 InitializeComponent();
18 this.btn1.Click += new RoutedEventHandler(btn1_Click);
19 }
20
21 void btn1_Click(object sender, RoutedEventArgs e)
22 {
23 //实例化委托
24 AsyncEventHandler async = new AsyncEventHandler(GetValue);
25 //开始异步
26 IAsyncResult ia = async.BeginInvoke("Hello World!", new AsyncCallback(CallBack), async);
27 }
28
29 /**//// <summary>
30 /// 异步完成CallBack方法
31 /// </summary>
32 /// <param name="result"></param>
33 private void CallBack(IAsyncResult result)
34 {
35 //转换返回值为委托类型
36 AsyncEventHandler async = (AsyncEventHandler)result.AsyncState;
37 //获得异步委托的返回值
38 string value = async.EndInvoke(result);
39 //实例化更新界面的委托
40 UpdateUI method = new UpdateUI(Update);
41 //开始界面委托
42 this.Dispatcher.Invoke(method, value);
43 }
44
45
46 /**//// <summary>
47 /// 更新界面
48 /// </summary>
49 /// <param name="value"></param>
50 private void Update(string value)
51 {
52 this.lbl1.Content = value;
53 }
54
55 /**//// <summary>
56 /// 调用的方法
57 /// </summary>
58 /// <param name="value"></param>
59 /// <returns></returns>
60 private string GetValue(string value)
61 {
62 Thread.Sleep(10000);
63 return "say:" + value;
64 }
65 }