Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Net how to realize asynchronous programming async and await

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of ".NET how to implement asynchronous programming async and await". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Await and async are found in the .NET Framework4.5 Framework and Clip5.0 syntax, while await and async are syntactic sugars.

Note:

1. Async appears in the method declaration, and any method can add async.

2. Await is placed in front of Task, async and await appear in pairs, only async is meaningless, and only await reports an error.

Only async is meaningless.

Only await reported the error.

3. Await can only be placed in front of task. The returned value of void is not recommended. Use Task instead. Task and Task can be used in combination with await, Task.WhenAny, Task.WhenAll, etc. Async Void can't.

Private static async Task Return () {/ / main thread executes Console.WriteLine ($"NoReturn Sleep before await,ThreadId= {Thread.CurrentThread.ManagedThreadId}"); TaskFactory taskFactory = new TaskFactory (); Task task = taskFactory.StartNew (()) = > {Console.WriteLine ($"NoReturn Sleep before,ThreadId= {Thread.CurrentThread.ManagedThreadId}"); Thread.Sleep (3000) Console.WriteLine ($"NoReturn Sleep after,ThreadId= {Thread.CurrentThread.ManagedThreadId}");}); / / the main thread returns here and executes the main thread task await task / / the thread of this callback is uncertain: the main thread may be a child thread or another thread Console.WriteLine ($"NoReturn Sleep after await,ThreadId= {Thread.CurrentThread.ManagedThreadId}");} "how .NET implements asynchronous programming async and await" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report