Fixed operation system error

修复异步操作系统的Task二次等待失败的问题。
pull/9/head
hevinci 2022-04-29 11:14:21 +08:00
parent c6440c9312
commit ca7b42981b
1 changed files with 22 additions and 23 deletions

View File

@ -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
}
}
/// <summary>
/// 异步操作任务
/// </summary>
public Task Task
{
get
{
if (_taskCompletionSource == null)
{
_taskCompletionSource = new TaskCompletionSource<object>();
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<object> _taskCompletionSource;
#endregion
}
}