WPF異步模式簡(jiǎn)單應(yīng)用方式介紹
WPF異步模式是一個(gè)比較復(fù)雜的實(shí)現(xiàn)過(guò)程。不過(guò)我們?cè)谶@篇文章中將會(huì)為大家介紹一下有關(guān)WPF異步模式簡(jiǎn)單的實(shí)現(xiàn)方法,方便大家理解。#t#
以WeatherForecast為例. 需求: 用戶在窗體上點(diǎn)擊一個(gè)按鈕, 程序去網(wǎng)絡(luò)上查詢天氣情況, 并把結(jié)果顯示在窗體上. 網(wǎng)絡(luò)查詢是一個(gè)耗時(shí)任務(wù), 在等待結(jié)果的同時(shí), 用戶將看到一個(gè)旋轉(zhuǎn)的時(shí)鐘動(dòng)畫(huà)表示程序正在查詢.
WPF異步模式為:
窗口類(lèi)MainWindow中有耗時(shí)函數(shù): string FetchWeatherFromInternet().
窗口類(lèi)MainWindow中包含一個(gè)函數(shù) UpdateUIWhenWeatherFetched(string result), 用于把任務(wù)結(jié)果顯示在界面上.
當(dāng)用戶點(diǎn)擊按鈕時(shí), 在 btnFetchWeather_Click() 中,
如果是同步調(diào)用, 代碼很簡(jiǎn)單, 如下:
- string result = this.
 
FetchWeatherFromInternet();- this.UpdateUserInterface( result );
 
現(xiàn)在需要異步執(zhí)行, 稍微麻煩點(diǎn), 需要用到3個(gè)delegate, 其中一個(gè)代表要執(zhí)行的任務(wù), 另一個(gè)代表任務(wù)結(jié)束后的callback, 還有一個(gè)代表交給UI執(zhí)行的任務(wù), 上述WPF異步模式代碼被替換成如下:
- // 代表要執(zhí)行的異步任務(wù)
 - Func<string> asyncAction =
 
this.FetchWeatherFromInternet();- // 處理異步任務(wù)的結(jié)果
 - Action<IAsyncResult> resultHandler =
 
delegate( IAsyncResult asyncResult )- {
 - //獲得異步任務(wù)的返回值, 這段代碼必須
 
在UI線程中執(zhí)行- string weather = asyncAction.
 
EndInvoke( asyncResult );- this.UpdateUIWhenWeatherFetched
 
( weather );- };
 - //代表異步任務(wù)完成后的callback
 - AsyncCallback asyncActionCallback =
 
delegate( IAsyncResult asyncResult )- {
 - this.Dispatcher.BeginInvoke
 
( DispatcherPriority.Background,
resultHandler, asyncResult );- };
 - //這是才開(kāi)始執(zhí)行異步任務(wù)
 - asyncAction.BeginInvoke
 
( asyncActionCallback, null );- private void ForecastButtonHandler
 
(object sender, RoutedEventArgs e)- {
 - this.UpdateUIWhenStartFetching
 
Weather();- //異步任務(wù)封裝在一個(gè)delegate中,
 
此delegate將運(yùn)行在后臺(tái)線程- Func<string> asyncAction = this.
 
FetchWeatherFromInternet;- //在UI線程中得到異步任務(wù)的返回值,
 
并更新UI //必須在UI線程中執(zhí)行 Action
<IAsyncResult> resultHandler =
delegate(IAsyncResult asyncResult)
{ string weather = asyncAction.EndInvoke
(asyncResult); this.UpdateUIWhenWeather
Fetched(weather); };- //異步任務(wù)執(zhí)行完畢后的callback, 此callback
 
運(yùn)行在后臺(tái)線程上. //此callback會(huì)異步調(diào)用
resultHandler來(lái)處理異步任務(wù)的返回值.- AsyncCallback asyncActionCallback =
 
delegate(IAsyncResult asyncResult)- {
 - this.Dispatcher.BeginInvoke
 
(DispatcherPriority.Background,
resultHandler, asyncResult);- };
 - //在UI線程中開(kāi)始異步任務(wù), //asyncAction
 
(后臺(tái)線程), asyncActionCallback(后臺(tái)線程)
和resultHandler(UI線程) //將被依次執(zhí)行- asyncAction.BeginInvoke(asyncAction
 
Callback, null);- }
 - private string FetchWeatherFromInternet()
 - {
 - // Simulate the delay from network access.
 - Thread.Sleep(4000);
 - String weather = "rainy";
 - return weather;
 - }
 - private void UpdateUIWhenStartFetching
 
Weather()- {
 - // Change the status this.fetchButton.
 
IsEnabled = false;- this.weatherText.Text = "";
 - }
 - private void UpdateUIWhenWeatherFetched
 
(string weather)- {
 - //Update UI text
 - this.fetchButton.IsEnabled = true;
 - this.weatherText.Text = weather;
 - }
 
以上就是對(duì)WPF異步模式實(shí)現(xiàn)的簡(jiǎn)單方法介紹。















 
 
 
 
 
 
 