We know that many interfaces in winrt are asynchronous, such as storagefile and mediacapture. They all end with "async. So what are the advantages of asynchronous programming? It may take several seconds for us to execute a function. If we use the synchronous method, the UI thread will be blocked, and the application will not respond to user operations, resulting in poor experience. With asynchronous programming, the application can continue to run and respond to the UI while waiting for completion.
In winrt, each language has its asynchronous programming mode:
Javascript: Promise object, then Function
C #: Future object, await Operator
VB: Future object, await Operator
Visual c ++: Task class,. Then Method
WinrtAsynchronous programming (C #)
C # asynchronous programming in winrt is relatively simple and requires several rules:
(1) The method body that calls the asynchronous API must be identified by async.
(2) The await operator is required to call the asynchronous API.
You can think of the await operator as telling the compiler that it is currently calling an asynchronous method, and the compiler can execute other work without waiting here. The advantage of await operator is that the Code is not complex and easy to understand and maintain.
Private async void createfile ()
{
Storagefolder = knownfolders. documentslibrary;
Storagefile = await storagefolder. createfileasync ("template.txt", creationcollisionoption. replaceexisting );
Outputtextblock. Text = storagefolder. Name + "has been created ";
}
WinrtAsynchronous programming (C ++)
C ++ asynchronous programming is more complex than C #. It mainly uses the asynchronous method through the task class defined in ppltask. h.
When the asynchronous winrt API is directly used in C ++, the task class and its related types and functions are preferred. They are included in the concurrency namespace and defined in ppltask. h.
(1) create a task using create_task
Auto createfiletadk = create_task (Folder-> createfileasync ("aa.txt", creationcollisionoption: replaceexisting ));
(2) operations performed when asynchronous completion is implemented using task: then
Createfiletadk. Then ([this] (storagefile ^ storagefilesample ){
String ^ filename = storagefilesample-> name;
});
Task: The task created and returned by the then function is called a continuation. The task runs only after the asynchronous operation is completed successfully. If an asynchronous operation causes an exception or cancels a task, the task will never be executed. The above is a simple asynchronous example. There is no asynchronous task in the continuation task. If we execute another asynchronous task in the continuation task, there may be another asynchronous task in the continuation of the asynchronous task, this series of tasks will form a task chain, one link and one link, each continuation will be executed only after the previous continuation task is completed, this will form a form: mytask. then (). then (). then ();
In the above example, after creating a file, I have to write content to the file. After writing the file, I have to give the user a prompt:
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 ();
});
});
Exception in capturing asynchronous tasks
We can get the task result through task: Get, which can get all the data that has been transmitted to the task.
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)
{
}
});