当前位置:网站首页>C # use awaiter
C # use awaiter
2022-07-05 03:39:00 【Dotnet cross platform】
Can be provided to any GetAwaiter Method and return awaiter Object using async keyword .awaiter use OnCompleted Method realization INotifyCompletion Interface . This method is called when the task is completed . The following code snippet is not used in a task await, But using tasks GetAwaiter Method .Task Class GetAwaiter Return to one TaskAwaiter. Use OnCompleted Method , Assign a local function to be called when the task is complete :
private static void CallerWithAwaiter()
{
TraceThreadAndTask($"starting {nameof(CallerWithAwaiter)}");
TaskAwaiter<string> awaiter = GreetingAsync("Matthias").GetAwaiter();
awaiter.OnCompleted(OnCompleteAwaiter);
void OnCompleteAwaiter()
{
Console.WriteLine(awaiter.GetResult());
TraceThreadAndTask($"ended {nameof(CallerWithAwaiter)}");
}
}
When running the application , The result is similar to that you use wait Keyword case :
starting CallerWithAwaiter in thread 2 and no task
running GreetingAsync in thread 3 and task 1
running Greeting in thread 3 and task 1
Hello,Matthias
ended CallerWithAwaiter in thread 3 and no task
The compiler the await Put all the code after the keyword into OnCompleted Method to convert await keyword .
边栏推荐
- 汇编-入门
- Solve the problem that sqlyog does not have a schema Designer
- Kubernetes -- cluster expansion principle
- Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
- Leetcode42. connect rainwater
- [positioning in JS]
- Azkaban actual combat
- [wp][入门]刷弱类型题目
- 51 independent key basic experiment
- Learning notes of raspberry pie 4B - IO communication (I2C)
猜你喜欢
Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
Share the newly released web application development framework based on blazor Technology
Machine learning experiment report 1 - linear model, decision tree, neural network part
The architect started to write a HelloWorld
An elegant program for Euclid‘s algorithm
Utilisation simple de devtools
Use of kubesphere configuration set (configmap)
Leetcode42. connect rainwater
[web Audit - source code disclosure] obtain source code methods and use tools
Pat class a 1160 forever (class B 1104 forever)
随机推荐
[105] Baidu brain map - Online mind mapping tool
Multimedia query
【PHP特性-变量覆盖】函数的使用不当、配置不当、代码逻辑漏洞
SQL injection exercise -- sqli Labs
A brief introduction to the behavior tree of unity AI
An elegant program for Euclid‘s algorithm
qrcode:将文本生成二维码
De debugging (set the main thread as hidden debugging to destroy the debugging Channel & debugger detection)
Kubernetes - Multi cluster management
[learning notes] month end operation -gr/ir reorganization
Performance of calling delegates vs methods
NPM introduction link symbolic link
Usage scenarios and solutions of ledger sharing
51 independent key basic experiment
Share the newly released web application development framework based on blazor Technology
SPI and IIC communication protocol
Kubernetes - identity and authority authentication
Linux Installation redis
VM in-depth learning (XXV) -class file overview
Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics