当前位置:网站首页>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 .
边栏推荐
- Pdf things
- Solve the problem that sqlyog does not have a schema Designer
- Zero foundation uses paddlepaddle to build lenet-5 network
- Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
- [luat-air105] 4.1 file system FS
- Unity implements the code of the attacked white flash (including shader)
- There is a question about whether the parallelism can be set for Flink SQL CDC. If the parallelism is greater than 1, will there be a sequence problem?
- Pat class a 1162 postfix expression
- The architect started to write a HelloWorld
- 腾讯云,实现图片上传
猜你喜欢
端口,域名,协议。
Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
grandMA2 onPC 3.1.2.5的DMX参数摸索
Zero foundation uses paddlepaddle to build lenet-5 network
[安洵杯 2019]不是文件上传
LeetCode146. LRU cache
VM in-depth learning (XXV) -class file overview
How to define a unified response object gracefully
The architect started to write a HelloWorld
SQL performance optimization skills
随机推荐
A brief introduction to the behavior tree of unity AI
Anchor free series network yolox source code line by line explanation four (a total of ten, ensure line by line explanation, after reading, you can change the network at will, not just as a participan
Clean up PHP session files
Accuracy problem and solution of BigDecimal
Daily question 2 12
Talk about the SQL server version of DTM sub transaction barrier function
Pdf things
Technology sharing swift defense programming
[groovy] groovy environment setup (download groovy | install groovy | configure groovy environment variables)
Binary heap implementation (priority queue implementation)
ICSI213/IECE213 Data Structures
DECLARE_ WAIT_ QUEUE_ HEAD、wake_ up_ Interruptible macro analysis
FBO and RBO disappeared in webgpu
1. Five layer network model
Share the newly released web application development framework based on blazor Technology
When sqlacodegen generates a model, how to solve the problem that the password contains special characters?
[positioning in JS]
How can we truncate the float64 type to a specific precision- How can we truncate float64 type to a particular precision?
Kubernetes - identity and authority authentication
Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation