https://docs.microsoft.com/zh-cn/dotnet/standard/asynchronous-programming-patterns/
.NET 提供了执行异步操作的三种模式:
-
基于任务的异步模式 (TAP),该模式使用单一方法表示异步操作的开始和完成。 TAP 是在 .NET Framework 4 中引入的。 这是在 .NET 中进行异步编程的推荐方法。 C# 中的 async 和 await 关键词以及 Visual Basic 中的 Async 和 Await 运算符为 TAP 添加了语言支持。 有关详细信息,请参阅基于任务的异步模式 (TAP)。
-
基于事件的异步模式 (EAP),是提供异步行为的基于事件的旧模型。 这种模式需要后缀为
Async
的方法,以及一个或多个事件、事件处理程序委托类型和EventArg
派生类型。 EAP 是在 .NET Framework 2.0 中引入的。 建议新开发中不再使用这种模式。 有关详细信息,请参阅基于事件的异步模式 (EAP)。 -
异步编程模型 (APM) 模式(也称为 IAsyncResult 模式),这是使用 IAsyncResult 接口提供异步行为的旧模型。 在这种模式下,同步操作需要
Begin
和End
方法(例如,BeginWrite
和EndWrite
以实现异步写入操作)。 不建议新的开发使用此模式。有关详细信息,请参阅异步编程模型 (APM)。
模式的比较
为了快速比较这三种模式的异步操作方式,请考虑使用从指定偏移量处起将指定量数据读取到提供的缓冲区中的Read
方法:
public class MyClass
{
public int Read(byte [] buffer, int offset, int count);
}
此方法对应的 TAP 将公开以下单个 ReadAsync
方法:
public class MyClass
{
public Task<int> ReadAsync(byte [] buffer, int offset, int count);
}
对应的 EAP 将公开以下类型和成员的集:
public class MyClass
{
public void ReadAsync(byte [] buffer, int offset, int count);
public event ReadCompletedEventHandler ReadCompleted;
}
对应的 APM 将公开 BeginRead
和 EndRead
方法:
public class MyClass
{
public IAsyncResult BeginRead(
byte [] buffer, int offset, int count,
AsyncCallback callback, object state);
public int EndRead(IAsyncResult asyncResult);
}
深入了解异步: https://docs.microsoft.com/zh-cn/dotnet/standard/async-in-depth
Unity Thread demo:https://answers.unity.com/questions/357033/unity3d-and-c-coroutines-vs-threading.html
Async-Await instead of coroutines in Unity 2017:http://www.stevevermeulen.com/index.php/2017/09/using-async-await-in-unity3d-2017/
Unitygem Loom thread(Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享):https://dsqiu.iteye.com/blog/2028503
Coroutine++:https://unitygem.wordpress.com/2016/01/27/coroutine/