From ca7b42981bed1c412f08ad82d02ab71fa1640a24 Mon Sep 17 00:00:00 2001 From: hevinci Date: Fri, 29 Apr 2022 11:14:21 +0800 Subject: [PATCH] Fixed operation system error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复异步操作系统的Task二次等待失败的问题。 --- .../OperationSystem/AsyncOperationBase.cs | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) 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