Files
KinginfoGateway/framework/ThingsGateway.Foundation/Common/WaitDataExAsync.cs
Kimdiego2098 b103f25c94 add dispose()
2023-09-15 13:23:39 +08:00

133 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://diego2098.gitee.io/thingsgateway-docs/
// QQ群605534569
//------------------------------------------------------------------------------
#endregion
namespace TouchSocket.Core
{
/// <summary>
/// 等待数据对象
/// </summary>
/// <typeparam name="T"></typeparam>
public class WaitDataExAsync<T> : DisposableObject, IWaitData<T>
{
private readonly AsyncAutoResetEvent m_asyncWaitHandle;
private volatile WaitDataStatus m_status;
/// <summary>
/// 构造函数
/// </summary>
public WaitDataExAsync()
{
this.m_asyncWaitHandle = new AsyncAutoResetEvent(false);
}
/// <inheritdoc/>
public WaitDataStatus Status { get => this.m_status; }
/// <inheritdoc/>
public T WaitResult { get; private set; }
/// <inheritdoc/>
public void Cancel()
{
this.m_status = WaitDataStatus.Canceled;
this.m_asyncWaitHandle.Set();
}
/// <inheritdoc/>
public void Reset()
{
this.m_status = WaitDataStatus.Default;
this.WaitResult = default;
this.m_asyncWaitHandle.Reset();
}
/// <inheritdoc/>
public bool Set()
{
this.m_status = WaitDataStatus.SetRunning;
return this.m_asyncWaitHandle.Set();
}
/// <inheritdoc/>
public bool Set(T waitResult)
{
this.WaitResult = waitResult;
this.m_status = WaitDataStatus.SetRunning;
return this.m_asyncWaitHandle.Set();
}
CancellationTokenRegistration registration = default;
/// <inheritdoc/>
public void SetCancellationToken(CancellationToken cancellationToken)
{
if (registration == default)
{
if (cancellationToken.CanBeCanceled)
registration = cancellationToken.Register(this.Cancel);
}
else
{
registration.Dispose();
if (cancellationToken.CanBeCanceled)
registration = cancellationToken.Register(this.Cancel);
}
}
/// <inheritdoc/>
public void SetResult(T result)
{
this.WaitResult = result;
}
/// <summary>
/// 等待指定时间
/// </summary>
/// <param name="timeSpan"></param>
/// <returns></returns>
public async Task<WaitDataStatus> WaitAsync(TimeSpan timeSpan)
{
if (!await this.m_asyncWaitHandle.WaitOneAsync(timeSpan))
{
this.m_status = WaitDataStatus.Overtime;
}
return this.m_status;
}
/// <summary>
/// 等待指定毫秒
/// </summary>
/// <param name="millisecond"></param>
public Task<WaitDataStatus> WaitAsync(int millisecond)
{
return this.WaitAsync(TimeSpan.FromMilliseconds(millisecond));
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
this.m_status = WaitDataStatus.Disposed;
this.WaitResult = default;
this.m_asyncWaitHandle.SafeDispose();
this.registration.Dispose();
base.Dispose(disposing);
}
}
/// <summary>
/// 等待数据对象
/// </summary>
public class WaitDataExAsync : WaitDataAsync<object>
{
}
}