C#5.0 .net 4.5示例
//filename: MathOperations.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestAsyncAwait { public class MathOperations { public static double MultiplyByTwo(double d) { return d * 2; } public static double Square(double d) { return d * d; } } } //filename: MyClass.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace TestAsyncAwait { public class MyClass { public MyClass() { //DisplayValue(); DisplayValueWithContinuationTask(); Console.WriteLine("MyClass() end."); } public Task<double> GetValueAsync(double num1, double num2) { return Task.Run<double>(() => { Thread.Sleep(1000); return num1 + num2; }); } public async void DisplayValue() { double result = await GetValueAsync(1, 2); Console.WriteLine("result is :" + result); } public void DisplayValueWithContinuationTask() { Task<double> task = GetValueAsync(1, 2); task.ContinueWith(t => { double result = t.Result; Console.WriteLine("result is :" + result); }); } } } //filename: Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace TestAsyncAwait { class Program { public static Tuple<int, int> Divide(int dividend, int divisor) { int result = dividend / divisor; int reminder = dividend % divisor; return Tuple.Create<int, int>(result, reminder); } private delegate string GetAString(); static void ProcessAndDispalyNumber(Func<double,double> action, double value){ double result = action(value); Console.WriteLine(string.Format("value={0},result={1}", value, result)); } #region 异步编程实验 static string Greeting(string name) { Thread.Sleep(3000); return string.Format("threadID:[{0}] says: Hello, {1}!", Thread.CurrentThread.ManagedThreadId, name); } static Task<string> GreetingAsync(string name) { return Task.Run<string>(() => { return Greeting(name); }); } async static void CallerWithAsync() { string result = await GreetingAsync("张三"); Console.WriteLine(result); } static void CallerWithContinuationWith() { Task<string> task = GreetingAsync("张三"); task.ContinueWith(t => { string result = t.Result; Console.WriteLine(result); }); } async static void MultipleAsyncMethods() { string result1 = await GreetingAsync("张三@"); string result2 = await GreetingAsync("李四@"); Console.WriteLine("Finished with 2 result: {0},{1}", result1, result2); } async static void MultipleAsyncMethodsWithCombinators1() { Task<string> t1 = GreetingAsync("张三1"); Task<string> t2 = GreetingAsync("李四1"); await Task.WhenAll(t1, t2); Console.WriteLine("Finished with 2 result: {0},{1}", t1.Result, t2.Result); } async static void MultipleAsyncMethodsWithCombinators2() { Task<string> t1 = GreetingAsync("张三2"); Task<string> t2 = GreetingAsync("李四2"); string[] result = await Task.WhenAll(t1, t2); Console.WriteLine("Finished with 2 result: {0},{1}", result[0], result[1]); } #endregion static void Main(string[] args) { /* CallerWithAsync(); CallerWithContinuationWith(); MultipleAsyncMethods(); MultipleAsyncMethodsWithCombinators1(); MultipleAsyncMethodsWithCombinators2(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ": Done."); */ ParallelLoopResult result = Parallel.For(0, 10, async i => { Console.WriteLine("{0}, task: {1}, thread: {2}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId); //Thread.Sleep(10); await Task.Delay(10); Console.WriteLine("{0}, task: {1}, thread: {2}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId); }); Console.WriteLine("Is completed: {0}", result.IsCompleted); /* MyClass mc = new MyClass(); Console.WriteLine("-----------------"); var result = Divide(5, 2); Console.WriteLine(string.Format("result={0},reminder={1}",result.Item1, result.Item2)); int x = 4; //GetAString func = new GetAString(x.ToString); GetAString func = x.ToString; Console.WriteLine(func()); Console.WriteLine(func.Invoke()); Func<double, double>[] operations = { MathOperations.MultiplyByTwo, MathOperations.Square }; foreach (var item in operations) { ProcessAndDispalyNumber(item, 3.14); } string midPart = ", middle part,"; Func<string, string> anonDel = delegate(string value) { string s = value + midPart; s += " last part"; return s; }; Console.WriteLine(anonDel("Hello")); Func<string, string> anonDel2 = (string value) => { string s = value + midPart; s += " last part. version 2"; return s; }; Console.WriteLine(anonDel2("Hello")); Func<string, string> anonDel3 = value => { string s = value + midPart; s += " last part. version 3"; return s; }; Console.WriteLine(anonDel3("Hello")); int someValue = 5; Func<int, int> f = p1 => p1 + someValue; Console.WriteLine(f(1)); someValue = 6; Console.WriteLine(f(1)); //var lists = new List<string>() { "1","2"}; dynamic dyn; dyn = 100; Console.WriteLine(dyn.GetType()); Console.WriteLine(dyn); dyn = "abc中国"; Console.WriteLine(dyn.GetType()); Console.WriteLine(dyn); */ Console.ReadKey(); } } }