标签:
比如需要下载一些比较大的文件,如果使用会UI卡顿,使用异步可以节省一些时间
下面是一些例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class Program { static void Main(string[] args) { System.Net.WebClient client = new System.Net.WebClient(); client.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(client_DownloadStringCompleted); //在异步资源下载操作完成时发生。 client.DownloadStringAsync(new Uri("https://www.baidu.com")); //异步下载地址 for (int i = 0; i < 10;i++ ) { Console.WriteLine("异步操作是执行此代码:"+i); } Console.ReadKey(); } public static void client_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e) { System.Net.WebClient client = sender as System.Net.WebClient; Console.WriteLine(e.Result); //异步处理的结果 } } }
上述代码运行的结果:
标签:
原文地址:http://www.cnblogs.com/linJie1930906722/p/5662059.html