我們知道,在WinRT中很多介面都是非同步,如StorageFile、MediaCapture等。它們都是以“Async”結尾。那使用非同步編程有什麼好處呢?估計大家都知道,當我們執行一個函數需要花費數秒鐘,若使用同步方法,則會阻止UI線程,應用將不會響應使用者操作,體驗性將會非常糟糕。而使用非同步編程,應用在等待完成時可以繼續運行並響應UI。
在WinRT中,每種語言都有其非同步編程的模式:
JavaScript : 承諾對象,then函數
C#: 將來對象,await運算子
VB: 將來對象,await運算子
Visual C++: task類,.then方法
WinRT 非同步編程(C#)
WinRT中的C#非同步編程相對較簡單,需要遵守幾個規則:
(1) 調用非同步API的方法體前需要以async標識
(2) 調用非同步API需要使用await運算子
Await運算子你可將其看作它告訴編譯器當前正在調用某個非同步方法呼叫,編譯器可以執行其它的工作,無需在此等待。作用await運算子的好處是代碼看上去不複雜,又易於理解和維護。
private async void CreateFile()
{
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile storageFile = await storageFolder.CreateFileAsync("template.txt",CreationCollisionOption.ReplaceExisting);
OutputTextBlock.Text = storageFolder.Name + "has been created";
}
WinRT 非同步編程(C++)
C++非同步編程相對C#較複雜些,主要通過ppltask.h中定義的任務類來使用非同步方法呼叫。
C++中直接使用非同步WinRT API時,首選task類及其相互關聯類型和函數,它們都包含在concurrency命名空間中,並且在ppltask.h中定義。
(1) 利用create_task建立一個任務
auto createFileTadk =create_task(folder->CreateFileAsync("aa.txt",CreationCollisionOption::ReplaceExisting));
(2) 利用task::then實現非同步完成時所執行的操作
createFileTadk.then([this](StorageFile^ storageFileSample) {
String^ filename=storageFileSample->Name;
});
task::then函數建立並返回的任務稱為延續,非同步工作成功完成後才會運行。若非同步作業引發異常,或取消任務,則延續永遠不會執行。以上是一個很簡單的非同步樣本,在延續任務中沒有非同步任務,若在延續中我們還會再次執行非同步任務,可能非同步任務的延續中還會再有非同步任務,這一系列的任務就會形成一個任務鏈,一環接一環,每個延續只有在前一個延續任務完成後才會執行,這就會形成這樣一種形式:mytask.then().then().then();
如上例操作,我需要在建立檔案後,還得往檔案寫入內容,寫完檔案我還得給使用者一個提示:
createFileTadk.then([this](StorageFile^ storageFileSample) ->IAsyncAction^ {
String^ userContent="abcdwfwe";
create_task(FileIO::WriteTextAsync(storageFileSample, userContent)).then([this, storageFileSample, userContent]()
{
MessageDialog^ message=ref new MessageDialog("File is completed create!");
message->ShowAsync();
});
});
捕獲非同步任務的異常
我們可以通過task::get擷取任務結果,其可以擷取已經傳輸到該任務的所有。
createFileTadk.then([this](StorageFile^ storageFileSample) ->IAsyncAction^ {
String^ userContent="abcdwfwe";
return FileIO::WriteTextAsync(storageFileSample, userContent);
}).then([](task<void> t){
try
{
t.get();
OutputDebugString(L"the content has been write into file!");
//tbText->Text="the content has been write into file";
}
catch(Platform::COMException^ e)
{
}
});