Get the App
SLTechnology News&Howtos  ›  Development  › 

Net how to realize asynchronous programming async and await

Shulou Source: shulou.com Published: 2022-06-01 06:38:52 10月03日 Update

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!

Tags: Thread only programming content meaning method more knowledge grammar no practical successful next task dilemma reality situation article way framework Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information MySQL MariaDB Microsoft