Timeout
Helps to compute timeout for asynchronous operations.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext.Threading
{
public readonly struct Timeout
{
private readonly DateTime created;
private readonly TimeSpan timeout;
internal bool Zero => timeout == TimeSpan.Zero;
public bool Expired => DateTime.Now - created > timeout;
public Timeout(TimeSpan timeout)
{
created = DateTime.Now;
this.timeout = timeout;
}
public void ThrowIfExpired()
{
if (Expired)
throw new TimeoutException();
}
public static bool operator true([In] [System.Runtime.CompilerServices.IsReadOnly] ref Timeout timeout)
{
return timeout.Expired;
}
public static bool operator false([In] [System.Runtime.CompilerServices.IsReadOnly] ref Timeout timeout)
{
return !timeout.Expired;
}
public static implicit operator TimeSpan([In] [System.Runtime.CompilerServices.IsReadOnly] ref Timeout timeout)
{
return timeout.timeout;
}
}
}