http://www.cnblogs.com/lekko/archive/2013/03/05/2944282.html
private static void SyncMethod(int input)
{
Console.WriteLine("进入同步操作!");
var result = SyancWork(input);
Console.WriteLine("最终结果{0}", result);
Console.WriteLine("退出同步操作!");
}
// 模拟耗时操作(同步方法)
private static int SyancWork(int val)
{
for (int i = 0; i < 5; ++i)
{
Console.WriteLine("耗时操作{0}", i);
Thread.Sleep(100);
val++;
}
return val;
}
// 异步操作
private static async void AsyncMethod(int input)
{
Console.WriteLine("进入异步操作!");
var result = await AsyncWork(input);
Console.WriteLine("最终结果{0}", result);
Console.WriteLine("退出异步操作!");
}
//模拟耗时操作(异步方法)
private static async Task<int> AsyncWork(int val)
{
for (int i = 0; i < 5; ++i)
{
Console.WriteLine("耗时操作{0}", i);
await Task.Delay(100);
val++;
}
return val;
}
static void Main(string[] args)
{
Console.WriteLine("同步方式测试开始!");
AsyncMethod(0);
Console.WriteLine("同步方式结束!");
Console.ReadKey();
}