原創|其它|編輯:郝浩|2012-08-22 21:59:09.000|閱讀 1561 次
概述:這是WPF 4.5的新特性介紹系列的第三部分。介紹了WPF4.5的Dispatcher的新的用法以及一些其它的新功能,并以代碼的形式讓讀者更容易深入理解。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
這是WPF 4.5的新特性介紹系列的第三部分。更多WPF4.5探秘系列文章
當你開始做的事情不同步時,Dispatcher有可能是最常用到的。因為這是從另一個線程來更新UI控件唯一的方法。
即使它很容易使用,WPF的團隊也為它添加了13種方法。特別是新的await keyword。在本文里,我們將探秘這些新方法。
新的“類”方法
有的過載將作為一個參數Func委托。在之前的版本里,在Dispatcher的可用方法里無法返回一些東西(除了void),但是在新版本里已實現了這一功能。
這種新方法是:
在WPF 4.5之前,返回一些東西,這段代碼應該寫為:
//The function which returns something (here of type object)
Func<object> myReturningObjectFunction = () =>
{//For example only !
return new object();
};
object returnedOject = null;
//A mock action to be abble to return the object
Action mockAction = () => { returnedOject = myReturningObjectFunction(); };
//Actually invoke the method
Dispatcher.CurrentDispatcher.Invoke(mockAction, null);
//Here I can use the returned object
現在,您可以使用這個編寫一個更容易維持的代碼:
public void CallingMethod()
{
object returnedOject = Dispatcher.CurrentDispatcher
.Invoke(MyReturningObjectFunction, null);
}
//This method can now live alone !
private object MyReturningObjectFunction()
{
//For example only !
return new object();
}
Await-ready !
WPF團隊已經注意到了一些新生的‘await’關鍵字,并且Dispatcher現在也可以支持這些關鍵字了。
以下是一些新的方法:
1. InvokeAsync(Action)
2. InvokeAsync(Action, DispatcherPriority)
3. InvokeAsync(Action, DispatcherPriority, CancellationToken)
4. InvokeAsync(Func)
5. InvokeAsync(Func, DispatcherPriority)
6. InvokeAsync(Func, DispatcherPriority, CancellationToken)
這些方法返回DispatcherOperation / DispatcherOperation類型的對象。然后您就可以在它本身或者是“Task”屬性里使用await關鍵字。
下面舉個例子:
public async void CallingMethod()
{
await Dispatcher.CurrentDispatcher.InvokeAsync(MyReturningObjectFunction);
}
同樣,你可以做一些同步,通過使用DispatcherOperationWait方法等待Dispatcher操作完成。這是TaskExtensions類的擴展方法(在System.Windows.Threading上都可以使用)。
DispatcherOperation<object> dispatcherOperation =
Dispatcher.CurrentDispatcher.InvokeAsync(MyReturningObjectFunction);
dispatcherOperation.Task.Wait();
最后將會出現一個內容為“Calling Task.Wait will result in a deadlock if the operation is queued on a calling thread. For more information about using a Task to perform asynchronous operations, see Task Parallelism (Task Parallel Library).”的免責申明/警告。
Cancellation
最后,您可能已經注意到CancellationToken類型的新參數。這是.NET 4 Cancellation Framework的一部分。在后臺中,Dispatcher操作將開始創建一個受到這個取消指令的管理Task對象。
如果Task沒有啟動即你的調度程序操作沒有啟動,那么它將不再啟動。如果它已經開始那么它不會被停止,并且繼續執行。
事實上,取消指令的停止與否完全取決于任務本身的運轉。然而,我沒有為給定的Action找到一種方法跳過Cancellation指令,而不再使用在第一段中關于返回對象提到過的相同的方法。
并沒有一定要使用取消指令的場景,但是我認為這是使異步關鍵字的工作的必要條件。
這種情況在demo中很難復制,但是我們還找到了一個工作例子:
//Create a token source
var cts = new CancellationTokenSource();
//Launch the cancel of the token
Task.Factory.StartNew(() => cts.Cancel());
//Launch an operation with priority normal
// this will delay the execution of the following Invoke call.
Dispatcher.BeginInvoke(new Action(() =>
{
int i = 0; while (i < 300)
//Update the UI to be sure to block the following
// action. Rendering will occur and takes precedence
{ i++; _counterTextBlock.Text = i.ToString(); }
}), null);
try
{
//Launch a task with the cancellation token
Dispatcher.Invoke(() => { while (true);},
DispatcherPriority.Background, cts.Token);
}
catch (OperationCanceledException ex)
{
//We are here because the token was cancelled
Console.WriteLine("The operation didn't even start !");
}
Bonus
另外還有兩個更容易使用的方法:
1. Invoke(Action)
2. Invoke(Action, DispatcherPriority, CancellationToken, TimeSpan)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡資源編譯