1、不适用异步的示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Diagnostics;namespace ConsoleApplication1
{class MyDownloadString{Stopwatch sw = new Stopwatch();private int CountCharacters(int id, string uriString){WebClient wc1 = new WebClient();Console.WriteLine("Starting call {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);string result = wc1.DownloadString(new Uri(uriString));Console.WriteLine(" Call {0} completed : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);return result.Length;}private void CountToALargeNumber(int id, int value){for (long i = 0; i < value; i++){}Console.WriteLine(" End counting {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);}public void DoRun(){const int largeNumber = 6000000;const string microsoft = "http://www.microsoft.com";const string baidu = "http://www.baidu.com";sw.Start();int t1 = CountCharacters(1, microsoft);int t2 = CountCharacters(2, baidu);CountToALargeNumber(1, largeNumber);CountToALargeNumber(2, largeNumber);CountToALargeNumber(3, largeNumber);CountToALargeNumber(4, largeNumber);Console.WriteLine("Chars in {0} :{1}", microsoft, t1);Console.WriteLine("Chars in {0} :{1}", baidu, t2);}}class Program{static void Main(string[] args){MyDownloadString mds = new MyDownloadString();mds.DoRun();}}}
运行结果:
2、使用异步的示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Diagnostics;namespace ConsoleApplication1
{class MyDownloadString{Stopwatch sw = new Stopwatch();private async Task<int> CountCharactersAsyc(int id, string uriString){WebClient wc1 = new WebClient();Console.WriteLine("Starting call {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);string result = await wc1.DownloadStringTaskAsync(new Uri(uriString));Console.WriteLine(" Call {0} completed : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);return result.Length;}private void CountToALargeNumber(int id, int value){for (long i = 0; i < value; i++){}Console.WriteLine(" End counting {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);}public void DoRun(){const int largeNumber = 6000000;const string microsoft = "http://www.microsoft.com";const string baidu = "http://www.baidu.com";sw.Start();Task<int> t1 = CountCharactersAsyc(1, microsoft);Task<int> t2 = CountCharactersAsyc(2, baidu);CountToALargeNumber(1, largeNumber);CountToALargeNumber(2, largeNumber);CountToALargeNumber(3, largeNumber);CountToALargeNumber(4, largeNumber);Console.WriteLine("Chars in {0} :{1}", microsoft, t1.Result);Console.WriteLine("Chars in {0} :{1}", baidu, t1.Result);}}class Program{static void Main(string[] args){MyDownloadString mds = new MyDownloadString();mds.DoRun();}}}
运行结果:
3、使用返回Task<int>对象的异步方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Diagnostics;namespace ConsoleApplication1
{static class DoAsyncStuff{private static int GetSum(int i1, int i2){Thread.Sleep(3000);return i1 + i2;}public static async Task<int> CalculateSumAsync(int i1, int i2){int sum = await Task.Run(() => GetSum(i1, i2));return sum;}}class Program{static void Main(string[] args){Stopwatch sw = new Stopwatch();ConsoleColor color = Console.ForegroundColor;sw.Start();int a1 = 5;int a2 = 6;int a3 = 2;int a4 = 3;Task<int> value = DoAsyncStuff.CalculateSumAsync(a1, a2);Task<int> value2 = DoAsyncStuff.CalculateSumAsync(a3, a4);Console.WriteLine("1)Starting... {0}ms", sw.Elapsed.TotalMilliseconds);Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("1)Result:[ {0}+{1}={2} ]", a1, a2, value.Result);Console.ForegroundColor = color;Console.WriteLine("1)Ended! {0}ms", sw.Elapsed.TotalMilliseconds);Console.WriteLine("2)Starting... {0}ms", sw.Elapsed.TotalMilliseconds);Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("2)Result:[ {0}+{1}={2} ]", a3, a4, value2.Result);Console.ForegroundColor = color;Console.WriteLine("2)Ended! {0}ms", sw.Elapsed.TotalMilliseconds);sw.Stop();}}}
运行结果:
以上程序实际上在两个DoAsyncStuff.CalculateSumAsync异步方法中分别执行a1+a2和a3+a4的运算,调用方法即Main方法中的代码将继续其进程,从异步方法中获取Task<int>的对象。当需要实际值时,就引用Task对象的Result属性。如果异步方法设置了该属性,调用方法就能获得该值并继续,否则它将暂停并等待该属性被设置,然后再继续执行。