diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs index fc34823..a37ad96 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Threading.Tasks; namespace YooAsset { @@ -106,6 +107,7 @@ namespace YooAsset public virtual void Destory() { IsDestroyed = true; + _taskCompletionSource = null; } /// @@ -197,31 +199,22 @@ namespace YooAsset /// /// 异步操作任务 /// - public System.Threading.Tasks.Task Task + public Task Task { get { - var handle = WaitHandle; - return System.Threading.Tasks.Task.Factory.StartNew(o => + if(_taskCompletionSource == null) { - handle.WaitOne(); - return AssetObject as object; - }, this); + _taskCompletionSource = new TaskCompletionSource(); + if (IsDone) + _taskCompletionSource.SetResult(this); + } + return _taskCompletionSource.Task; } } #region 异步编程相关 - 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; - } - } + private TaskCompletionSource _taskCompletionSource; protected void InvokeCompletion() { // 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。 @@ -233,7 +226,9 @@ namespace YooAsset hande.InvokeCallback(); } } - _waitHandle?.Set(); + + if(_taskCompletionSource != null) + _taskCompletionSource.TrySetResult(this); } #endregion }