diff --git a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs index 71f7b18..a342fa3 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Threading.Tasks; namespace YooAsset { @@ -48,12 +49,31 @@ namespace YooAsset } } + /// + /// 异步操作任务 + /// + public Task Task + { + get + { + if (_taskCompletionSource == null) + { + _taskCompletionSource = new TaskCompletionSource(); + if (IsDone) + _taskCompletionSource.SetResult(null); + } + return _taskCompletionSource.Task; + } + } + internal abstract void Start(); internal abstract void Update(); internal void Finish() { _callback?.Invoke(this); - _waitHandle?.Set(); + + if (_taskCompletionSource != null) + _taskCompletionSource.TrySetResult(null); } #region 异步编程相关 @@ -66,28 +86,7 @@ namespace YooAsset } public object Current => null; - private System.Threading.EventWaitHandle _waitHandle; - private System.Threading.WaitHandle WaitHandle - { - get - { - if (_waitHandle == null) - _waitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset); - _waitHandle.Reset(); - return _waitHandle; - } - } - public System.Threading.Tasks.Task Task - { - get - { - var handle = WaitHandle; - return System.Threading.Tasks.Task.Factory.StartNew(o => - { - handle.WaitOne(); - }, this); - } - } + private TaskCompletionSource _taskCompletionSource; #endregion } } \ No newline at end of file