IT评测·应用市场-qidao123.com

标题: C#中如何使用异步编程 [打印本页]

作者: 梦见你的名字    时间: 2025-1-17 13:51
标题: C#中如何使用异步编程
在 C# 中,异步编程主要通过 async 和 await 关键字来实现。异步编程的目标是让程序在执行耗时操纵(如 I/O 操纵、网络请求等)时不会阻塞主线程,从而提高程序的性能。
1. 异步编程的核心概念

async 关键字

await 关键字

2. 异步编程的根本语法

定义异步方法
  1. public async Task GetDataAsync()
  2. {
  3.     // 异步操作
  4.     await Task.Delay(1000); // 模拟耗时操作
  5. }
复制代码
调用异步方法
  1. public async Task CallAsyncMethod()
  2. {
  3.     await GetDataAsync(); // 等待异步方法完成
  4.     Console.WriteLine("异步方法已调用.");
  5. }
复制代码
3. 异步编程的使用场景

I/O 密集型操纵

UI 应用程序

Web 应用程序

并行任务

4. 异步编程的示例

1:简朴的异步方法
  1. using System;
  2. using System.Threading.Tasks;
  3. class Program
  4. {
  5.     static async Task Main(string[] args)
  6.     {
  7.         Console.WriteLine("开始异步操作...");
  8.         await DoSomethingAsync();
  9.         Console.WriteLine("异步操作已完成.");
  10.     }
  11.     static async Task DoSomethingAsync()
  12.     {
  13.         await Task.Delay(2000); // 模拟耗时操作(2秒)
  14.         Console.WriteLine("DoSomethingAsync方法异步任务已完成.");
  15.     }
  16. }
复制代码
输出:
  1. 开始异步操作...
  2. DoSomethingAsync方法异步任务已完成.
  3. 异步操作已完成.
复制代码
2:异步文件读写
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. class Program
  5. {
  6.     static async Task Main(string[] args)
  7.     {
  8.         string filePath = "task-test.txt";
  9.         string content = "hello async";
  10.         // 异步写入文件
  11.         await WriteFileAsync(filePath, content);
  12.         Console.WriteLine("文件已写入");
  13.         // 异步读取文件
  14.         string readContent = await ReadFileAsync(filePath);
  15.         Console.WriteLine("文件内容: " + readContent);
  16.     }
  17.     static async Task WriteFileAsync(string filePath, string content)
  18.     {
  19.         await File.WriteAllTextAsync(filePath, content);
  20.     }
  21.     static async Task<string> ReadFileAsync(string filePath)
  22.     {
  23.         return await File.ReadAllTextAsync(filePath);
  24.     }
  25. }
复制代码
输出:
  1. 文件已写入
  2. 文件内容: hello async
复制代码
3:异步网络请求
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading.Tasks;
  4. class Program
  5. {
  6.     static async Task Main(string[] args)
  7.     {
  8.         string url = "https://xxxxx.com";
  9.         string content = await DownloadContentAsync(url);
  10.         Console.WriteLine("Content: " + content);
  11.     }
  12.     static async Task<string> DownloadContentAsync(string url)
  13.     {
  14.         using (HttpClient client = new HttpClient())
  15.         {
  16.             return await client.GetStringAsync(url);
  17.         }
  18.     }
  19. }
复制代码
4:并行异步任务
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading.Tasks;
  4. class Program
  5. {
  6.     static async Task Main(string[] args)
  7.     {
  8.         Task<string> task1 = DownloadContentAsync("https://xxx.com");
  9.         Task<string> task2 = DownloadContentAsync("https://xxx2.com");
  10.         string[] results = await Task.WhenAll(task1, task2);
  11.         Console.WriteLine("Task 1 result: " + results[0]);
  12.         Console.WriteLine("Task 2 result: " + results[1]);
  13.     }
  14.     static async Task<string> DownloadContentAsync(string url)
  15.     {
  16.         using (HttpClient client = new HttpClient())
  17.         {
  18.             return await client.GetStringAsync(url);
  19.         }
  20.     }
  21. }
复制代码
5. 异步编程的注意事项

  1. try
  2. {
  3.     await GetDataAsync();
  4. }
  5. catch (Exception ex)
  6. {
  7.     Console.WriteLine("Error: " + ex.Message);
  8. }
复制代码
6. 异步编程的好处

总结

C# 中的异步编程通过 async 和 await 关键字实现,可以或许显著提高程序的相应性和性能。它特别实用于 I/O 密集型操纵、UI 应用程序和 Web 应用程序等场景。通过公道使用异步编程,可以编写出高效、简洁且易于维护的代码。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4