C#入门教程Async和Await

2023-01-11 0 240

借助于 C# 中的 async 和 await URL,触发器程式设计十分盛行。当他们在处置 UI 时,在点选按键时,他们采用了三个长天数运转的方式,比如说加载三个大文档或其它须要极短天数的小东西,在此种情况下,整座插件要等候顺利完成整座各项任务。换言之,假如博戈达插件中的任何人民主化被堵塞,整座插件单厢被堵塞,他们的插件会暂停积极响应,直至整座各项任务顺利完成。

触发器程式设计在此种情况下十分管用。透过采用触发器程式设计,应用程序能竭尽全力进行不倚赖于整座各项任务顺利完成的其它组织工作。

在 async 和 await URL的协助下,他们将以更少的不懈努力赢得现代触发器程式设计的大部份益处。

假定他们依次采用 Method1 和 Method2 这两个方式,因而这三个方式彼此之间倚赖,Method1 须要极短天数就可以顺利完成它的各项任务。在博戈达程式设计中,它会竭尽全力执行第二个 Method1 并等候该方式顺利完成,接着再竭尽全力执行 Method2。因而,即便三种方式不相互倚赖,这也是三个费时的操作过程。

他们能采用单纯的缓存程式设计博戈达运转大部份方式,但它会堵塞 UI 并等候顺利完成大部份各项任务。为的是化解这个问题,他们不得已在现代的程式设计中撰写太少的标识符,但假如他们采用 async 和 await URL,他们将在更少的标识符中获得软件系统。

除此之外,他们将看见更多实例,假如任何人第二个 Method,即使 Method3 具备 method1 的倚赖亲密关系,所以它将在 await URL的协助下等候 Method1 的顺利完成。

C# 中的 Async 和 await 是标识符记号,用作记号各项任务顺利完成后命令行需从何方恢复正常的标识符边线。

让他们从介绍程式设计基本概念的前述实例开始。

C# async await 的标识符实例

class Program { static void Main(string[] args) { Method1(); Method2(); Console.ReadKey(); } public static async Task Method1() { await Task.Run(() => { for (int i = 0; i < 100; i++) { Console.WriteLine(” Method 1″); // Do something Task.Delay(100).Wait(); } }); } public static void Method2() { for (int i = 0; i < 25; i++) { Console.WriteLine(” Method 2″); // Do something Task.Delay(100).Wait(); } } }

上面给出的标识符中,Method1 和 Method2 不相互倚赖,他们从 Main 方式调用。

在这里,我们能清楚地看见 Method1 和 Method2 并没有相互等候。

C#入门教程Async和Await

Method1 将总长度作为整数值返回,他们将参数作为 Method3 中的长度传递,该参数来自 Method1。

在这里,他们要在 Method3 中传递参数之前采用 await URL,为此,他们要采用调用方式中的 async URL。

假如他们采用 C# 7 或更低版本,所以他们不能在控制台插件的 Main 方式中采用 async URL,即使它会给出以下错误。

C#入门教程Async和Await

他们将创建三个名为 callMethod 的新方式,在此方式中,他们将依次将大部份方式调用为 Method1、Method2 和 Method3。

class Program { static void Main(string[] args) { callMethod(); Console.ReadKey(); } public static async void callMethod() { Task<int> task = Method1(); Method2(); int count = await task; Method3(count); } public static async Task<int> Method1() { int count = 0; await Task.Run(() => { for (int i = 0; i < 100; i++) { Console.WriteLine(” Method 1″); count += 1; } }); return count; } public static void Method2() { for (int i = 0; i < 25; i++) { Console.WriteLine(” Method 2″); } } public static void Method3(int count) { Console.WriteLine(“Total count is “ + count); } }
class Program { static async Task Main(string[] args) { await callMethod(); Console.ReadKey(); } public static async Task callMethod() { Method2(); var count = await Method1(); Method3(count); } public static async Task<int> Method1() { int count = 0; await Task.Run(() => { for (int i = 0; i < 100; i++) { Console.WriteLine(” Method 1″); count += 1; } }); return count; } public static void Method2() { for (int i = 0; i < 25; i++) { Console.WriteLine(” Method 2″); } } public static void Method3(int count) { Console.WriteLine(“Total count is “ + count); } }

在上面给出的标识符中,Method3 须要三个参数,即 Method1 的返回类型。在这里,await 关键字在 Method1 各项任务顺利完成的等候中起着至关重要的作用。

C#入门教程Async和Await

.NET Framework 4.5 中有一些支持 API,Windows 运转时包含支持触发器程式设计的方式。

他们能借助于 async 和 await URL在实时项目中采用大部份这些,以更快地竭尽全力执行各项任务。

一些包含触发器方式的 API 是 HttpClient、SyndicationClient、StorageFile、StreamWriter、StreamReader、XmlReader、MediaCapture、BitmapEncoder、BitmapDecoder 等。

长度。

class Program { static void Main() { Task task = new Task(CallMethod); task.Start(); task.Wait(); Console.ReadLine(); } static async void CallMethod() { string filePath = “E:\\sampleFile.txt”; Task<int> task = ReadFile(filePath); Console.WriteLine(” Other Work 1″); Console.WriteLine(” Other Work 2″); Console.WriteLine(” Other Work 3″); int length = await task; Console.WriteLine(” Total length: “ + length); Console.WriteLine(” After work 1″); Console.WriteLine(” After work 2″); } static async Task<int> ReadFile(string file) { int length = 0; Console.WriteLine(” File reading is stating”); using (StreamReader reader = new StreamReader(file)) { // Reads all characters from the current position to the end of the stream asynchronously // and returns them as one string. string s = await reader.ReadToEndAsync(); length = s.Length; } Console.WriteLine(” File reading is completed”); return length; } }

在他们的 sampleText.txt 中,该文档包含的字符太少,因而加载大部份字符须要极短天数。

们采用了 await URL,他们将采用下面给出的标识符行的返回值。

C#入门教程Async和Await

推荐一款WPF MVVM框架开源项目:Newbeecoder.UI

C#入门教程Async和Await

Demo下载:

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务