1.
异步任务:async和await关键字知识编译器功能,编译器会用Task类创建代码。如果不使用这两个关键字,也可以使用Task类的方法来实现,只是这样比较麻烦。async只能用于返回Task或者void的方法。他不能用于程序的入口点,既Main方法不能用async修饰,await只能用于返回Task的方法
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
static class Program
{
static void Main(string[] args)
{
//这种异步的写法不会阻塞主线程,使程序继续执行下去
CallerWithAsync();
CallerWithAsync();
CallerWithAsync();
Console.WriteLine("111111111111111111111111");
Console.ReadKey();
}
/// <summary>
/// 睡眠3s后,返回一个字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string Greeting(string str)
{
Thread.Sleep(3000);
return str;
}
/// <summary>
/// 一个异步的方法,定义了一个返回字符串的任务
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static Task<string> GreetAsync(string str)
{
return Task.Run<string>(() => { return Greeting(str); });
}
/// <summary>
/// 使用await关键字来调用返回字符串的异步方法
/// 使用await关键字需要有用async修饰的方法,在GreetAsync方法完成之前方法不会继续执行,但是不影响CallerWithAsync的继续调用
/// </summary>
private static async void CallerWithAsync()
{
string result = await GreetAsync("lalalala");
Console.WriteLine(result);
}
}
}
2.延续任务:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
static class Program
{
static void Main(string[] args)
{
//这种异步的写法不会阻塞主线程,使程序继续执行下去
CallerWithCountinuationTask();
Console.ReadKey();
}
/// <summary>
/// 睡眠3s后,返回一个字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string Greeting(string str)
{
Thread.Sleep(3000);
return str;
}
/// <summary>
/// 一个异步的方法,定义了一个返回字符串的任务
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static Task<string> GreetAsync(string str)
{
return Task.Run<string>(() => { return Greeting(str); });
}
/// <summary>
/// 使用await关键字来调用返回字符串的异步方法
/// 使用await关键字需要有用async修饰的方法,在GreetAsync方法完成之前方法不会继续执行,但是不影响CallerWithAsync的继续调用
/// </summary>
private static async void CallerWithAsync()
{
string result = await GreetAsync("lalalala");
Console.WriteLine(result);
}
/// <summary>
/// 延续任务
/// </summary>
private static async void CallerWithCountinuationTask()
{
Task<string> result = GreetAsync("lalalala");
await result.ContinueWith(x =>
{
string t1 = x.Result;
Console.WriteLine(t1);
});
}
}
}
3.转换异步模式
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
static class Program
{
/// <summary>
/// 定义一个委托
/// </summary>
private static Func<string, string> _greetInvoker = Greeting;
static void Main(string[] args)
{
ConvertAsyncPattern();
Console.WriteLine("main");
Console.ReadKey();
}
/// <summary>
/// 睡眠3s后,返回一个字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string Greeting(string str)
{
Thread.Sleep(3000);
return str;
}
private static IAsyncResult BeginGreeting(string name, AsyncCallback callback, object state)
{
return _greetInvoker.BeginInvoke("asd", callback, state);
}
private static string EndGreeting(IAsyncResult ar)
{
return _greetInvoker.EndInvoke(ar);
}
private static async void ConvertAsyncPattern()
{
string s = await Task<string>.Factory.FromAsync<string>(BeginGreeting, EndGreeting, "12312312312", null);
Console.WriteLine(s);
}
}
}