Deadline Struct
Represents an instant on a clock, after which an operation gives up.
public readonly struct Deadline : IEquatable<Deadline>Remarks
The clock of a deadline is a TimeProvider. A deadline measures time with TimeProvider.GetTimestamp, which is monotonic, so a change of the system date does not move a deadline.
A deadline is a value. A method copies it, and passes it down a call chain, without an allocation.
The default value is Deadline.Eternal, a deadline that never expires, as the default CancellationToken is never canceled.
Two deadlines are equal when they have the same clock and the same instant. Deadline.Eternal and Deadline.Expired have no instant, and each is equal to itself alone. A deadline that has expired on a clock is not equal to Deadline.Expired.
- Implements
Properties
| Name | Description |
|---|---|
| Eternal | Gets a deadline that never expires. |
| Expired | Gets a deadline that has expired. |
| HasExpired | Gets a value indicating whether this deadline has expired. |
| IsEternal | Gets a value indicating whether this deadline never expires. |
| RemainingTime | Gets the time that remains until this deadline expires. |
| TimeProvider | Gets the clock of this deadline. |
Eternal
Expired
HasExpired
IsEternal
RemainingTime
Gets the time that remains until this deadline expires.
public TimeSpan RemainingTime { get; }Property value
TimeSpan The time until this deadline expires; TimeSpan.Zero if it has expired; Timeout.InfiniteTimeSpan if it never expires.
Remarks
The value follows the rules of the BCL for a timeout: TimeSpan.Zero for no wait, and Timeout.InfiniteTimeSpan for a wait without a time limit.
A member of the BCL that takes a timeout, such as Task.Delay, truncates the timeout to a millisecond, and rejects a timeout above its maximum. With this value as its timeout, such a member can therefore give up before this deadline expires, or throw an exception. Deadline.WaitAsync, Deadline.CreateCancellationTokenSource, and the WaitAsync methods of TaskExtensions round the remaining time up, and accept a deadline however far it is.
The value is rounded up to a tick, so it is TimeSpan.Zero only if Deadline.HasExpired is true.
A time longer than TimeSpan.MaxValue is reported as TimeSpan.MaxValue.
TimeProvider
Gets the clock of this deadline.
public TimeProvider TimeProvider { get; }Property value
TimeProvider The TimeProvider that this deadline measures time with. For Deadline.Eternal and Deadline.Expired, which depend on no clock, the value is TimeProvider.System.
Methods
| Name | Description |
|---|---|
| Compare(Deadline, Deadline) | Compares two deadlines and returns an integer that indicates whether the first expires before, together with, or after the second. |
| CreateCancellationTokenSource() | Creates a CancellationTokenSource that is canceled when this deadline expires. |
| EarliestOf(Deadline, Deadline) | Returns the one of two deadlines that expires first. |
| EarliestOf(Deadline, Deadline, Deadline) | Returns the one of three deadlines that expires first. |
| EarliestOf(Deadline, Deadline, Deadline, Deadline) | Returns the one of four deadlines that expires first. |
| EarliestOf(IEnumerable<Deadline>) | Returns the deadline that expires first in a sequence of deadlines. |
| Equals(Deadline) | Determines whether this deadline and a specified deadline are equal. |
| Equals(object?) | Indicates whether this instance and a specified object are equal. |
| GetHashCode() | Returns the hash code for this instance. |
| LatestOf(Deadline, Deadline) | Returns the one of two deadlines that expires last. |
| LatestOf(Deadline, Deadline, Deadline) | Returns the one of three deadlines that expires last. |
| LatestOf(Deadline, Deadline, Deadline, Deadline) | Returns the one of four deadlines that expires last. |
| LatestOf(IEnumerable<Deadline>) | Returns the deadline that expires last in a sequence of deadlines. |
| SetAfter(Deadline, TimeSpan) | Creates a deadline that expires a specified time after a specified deadline, on the same clock. |
| SetAt(DateTimeOffset) | Creates a deadline that expires at a specified date and time, as the system clock reads them at the call. |
| SetAt(DateTimeOffset, TimeProvider) | Creates a deadline that expires at a specified date and time, as a specified clock reads them at the call. |
| SetBefore(Deadline, TimeSpan) | Creates a deadline that expires a specified time before a specified deadline, on the same clock. |
| SetIn(TimeSpan) | Creates a deadline that expires a specified time after the call, on the system clock. |
| SetIn(TimeSpan, TimeProvider) | Creates a deadline that expires a specified time after the call, on a specified clock. |
| ThrowIfExpired() | Throws a TimeoutException if this deadline has expired. |
| ToString() | Returns a string that describes the state of this deadline. |
| WaitAsync(CancellationToken) | Asynchronously waits until this deadline expires. |
Compare(Deadline, Deadline)
Compares two deadlines and returns an integer that indicates whether the first expires before, together with, or after the second.
Parameters
Returns
int A negative number if first expires before second; zero if the two deadlines expire together; a positive number if first expires after second.
Remarks
If the two deadlines have the same clock, this method compares their instants, and does not read the clock. The result is zero only if the two deadlines are equal.
If the two deadlines have different clocks, this method reads both clocks, and compares the Deadline.RemainingTime of the two deadlines. The result is zero if the remaining times are the same, although the two deadlines are not equal. In particular, it is zero if both deadlines have expired. The result is correct at a later time only if both clocks run at the same rate.
Deadline.Eternal expires after every other deadline. Deadline.Expired expires before every other deadline, even one that has expired on a clock.
CreateCancellationTokenSource()
Creates a CancellationTokenSource that is canceled when this deadline expires.
public CancellationTokenSource CreateCancellationTokenSource()Returns
CancellationTokenSource A new CancellationTokenSource, which the caller disposes.
Remarks
Use this method to pass this deadline to a member that takes a CancellationToken alone. To also cancel the operation when another token is signaled, link the two tokens with CancellationTokenSource.CreateLinkedTokenSource.
If this deadline has expired, the source is canceled when this method returns. If this deadline never expires, this method starts no timer, and only the caller cancels the source.
This method sets a timer of the clock to a whole number of milliseconds. The timer can fire up to one millisecond after this deadline expires, or more when it is late.
If more than Int32.MaxValue milliseconds, about 24.8 days, remain until this deadline expires, this method starts no timer. The source is then not canceled when this deadline expires.
EarliestOf overloads
EarliestOf(Deadline, Deadline)
Returns the one of two deadlines that expires first.
Parameters
Returns
Deadline The parameter that expires first.
Remarks
This method orders deadlines as Deadline.Compare does. If Deadline.Compare reports the two deadlines as expiring together, this method returns first.
EarliestOf(Deadline, Deadline, Deadline)
Returns the one of three deadlines that expires first.
Parameters
firstDeadline- The first deadline to compare.
secondDeadline- The second deadline to compare.
thirdDeadline- The third deadline to compare.
Returns
Deadline The parameter that expires first.
Remarks
This method orders deadlines as Deadline.Compare does. Of the deadlines that Deadline.Compare reports as expiring together, it returns the first parameter.
EarliestOf(Deadline, Deadline, Deadline, Deadline)
Returns the one of four deadlines that expires first.
Parameters
firstDeadline- The first deadline to compare.
secondDeadline- The second deadline to compare.
thirdDeadline- The third deadline to compare.
fourthDeadline- The fourth deadline to compare.
Returns
Deadline The parameter that expires first.
Remarks
This method orders deadlines as Deadline.Compare does. Of the deadlines that Deadline.Compare reports as expiring together, it returns the first parameter.
EarliestOf(IEnumerable<Deadline>)
Returns the deadline that expires first in a sequence of deadlines.
public static Deadline EarliestOf(params IEnumerable<Deadline> deadlines)Parameters
deadlinesIEnumerable<Deadline>- The deadlines to compare.
Returns
Deadline The deadline in deadlines that expires first, or Deadline.Eternal if deadlines is empty.
Exceptions
ArgumentNullExceptiondeadlinesisnull.
Remarks
This method orders deadlines as Deadline.Compare does. Of the deadlines that Deadline.Compare reports as expiring together, it returns the first in the sequence.
Equals overloads
Equals(Deadline)
Equals(object?)
GetHashCode()
LatestOf overloads
LatestOf(Deadline, Deadline)
Returns the one of two deadlines that expires last.
Parameters
Returns
Deadline The parameter that expires last.
Remarks
This method orders deadlines as Deadline.Compare does. If Deadline.Compare reports the two deadlines as expiring together, this method returns first.
LatestOf(Deadline, Deadline, Deadline)
Returns the one of three deadlines that expires last.
Parameters
firstDeadline- The first deadline to compare.
secondDeadline- The second deadline to compare.
thirdDeadline- The third deadline to compare.
Returns
Deadline The parameter that expires last.
Remarks
This method orders deadlines as Deadline.Compare does. Of the deadlines that Deadline.Compare reports as expiring together, it returns the first parameter.
LatestOf(Deadline, Deadline, Deadline, Deadline)
Returns the one of four deadlines that expires last.
Parameters
firstDeadline- The first deadline to compare.
secondDeadline- The second deadline to compare.
thirdDeadline- The third deadline to compare.
fourthDeadline- The fourth deadline to compare.
Returns
Deadline The parameter that expires last.
Remarks
This method orders deadlines as Deadline.Compare does. Of the deadlines that Deadline.Compare reports as expiring together, it returns the first parameter.
LatestOf(IEnumerable<Deadline>)
Returns the deadline that expires last in a sequence of deadlines.
public static Deadline LatestOf(params IEnumerable<Deadline> deadlines)Parameters
deadlinesIEnumerable<Deadline>- The deadlines to compare.
Returns
Deadline The deadline in deadlines that expires last, or Deadline.Expired if deadlines is empty.
Exceptions
ArgumentNullExceptiondeadlinesisnull.
Remarks
This method orders deadlines as Deadline.Compare does. Of the deadlines that Deadline.Compare reports as expiring together, it returns the first in the sequence.
SetAfter(Deadline, TimeSpan)
Creates a deadline that expires a specified time after a specified deadline, on the same clock.
Parameters
deadlineDeadline- The deadline that the new deadline follows.
durationTimeSpan- The time from the instant of
deadlineto the instant of the new deadline, orTimeout.InfiniteTimeSpanfor a deadline that never expires.
Returns
Deadline A deadline on the clock of deadline, or Deadline.Eternal if duration is Timeout.InfiniteTimeSpan. If deadline is Deadline.Eternal or Deadline.Expired, the return value is deadline.
Exceptions
ArgumentOutOfRangeExceptiondurationis negative, and is notTimeout.InfiniteTimeSpan.
Remarks
This method adds duration to the instant of deadline, and does not read the clock. If duration is TimeSpan.Zero, the new deadline is equal to deadline.
Deadline.Eternal and Deadline.Expired have no instant. This method returns each of them as it is, even if duration is Timeout.InfiniteTimeSpan.
If the clock has no timestamp for the new instant, because duration is too long, this method returns Deadline.Eternal.
For a deadline that expires before deadline, call Deadline.SetBefore.
SetAt overloads
SetAt(DateTimeOffset)
Creates a deadline that expires at a specified date and time, as the system clock reads them at the call.
public static Deadline SetAt(DateTimeOffset instant)Parameters
instantDateTimeOffset- The date and time at which the deadline expires.
Returns
Deadline A deadline on TimeProvider.System.
Remarks
This method reads the date and time of the clock once, and sets the deadline in the time that remains until instant. If instant is not in the future, the deadline has expired.
The deadline does not follow a later change of the system date. After such a change, the deadline no longer expires at instant. For example, a device without a real-time clock corrects its date when it first reaches a time server. A deadline set before the correction is wrong by the size of the correction.
If the clock has no timestamp for instant, this method returns Deadline.Eternal for an instant in the future, and Deadline.Expired for an instant in the past.
SetAt(DateTimeOffset, TimeProvider)
Creates a deadline that expires at a specified date and time, as a specified clock reads them at the call.
public static Deadline SetAt(DateTimeOffset instant, TimeProvider timeProvider)Parameters
instantDateTimeOffset- The date and time at which the deadline expires.
timeProviderTimeProvider- The clock of the deadline.
Returns
Deadline A deadline on timeProvider.
Exceptions
ArgumentNullExceptiontimeProviderisnull.ArgumentException- The
TimeProvider.TimestampFrequencyoftimeProvideris zero or negative.
Remarks
This method reads the date and time of timeProvider once, and sets the deadline in the time that remains until instant. If instant is not in the future, the deadline has expired.
The deadline does not follow a later change of the date of timeProvider. After such a change, the deadline no longer expires at instant. For example, a device without a real-time clock corrects its date when it first reaches a time server. A deadline set before the correction is wrong by the size of the correction.
If timeProvider has no timestamp for instant, this method returns Deadline.Eternal for an instant in the future, and Deadline.Expired for an instant in the past.
SetBefore(Deadline, TimeSpan)
Creates a deadline that expires a specified time before a specified deadline, on the same clock.
Parameters
deadlineDeadline- The deadline that the new deadline precedes.
durationTimeSpan- The time from the instant of the new deadline to the instant of
deadline, orTimeout.InfiniteTimeSpanfor a deadline that has expired.
Returns
Deadline A deadline on the clock of deadline, or Deadline.Expired if duration is Timeout.InfiniteTimeSpan. If deadline is Deadline.Eternal or Deadline.Expired, the return value is deadline.
Exceptions
ArgumentOutOfRangeExceptiondurationis negative, and is notTimeout.InfiniteTimeSpan.
Remarks
This method subtracts duration from the instant of deadline, and does not read the clock. The new deadline may have expired already. If duration is TimeSpan.Zero, the new deadline is equal to deadline.
Deadline.Eternal and Deadline.Expired have no instant. This method returns each of them as it is, even if duration is Timeout.InfiniteTimeSpan.
If the clock has no timestamp for the new instant, because duration is too long, this method returns Deadline.Expired.
For a deadline that expires after deadline, call Deadline.SetAfter.
SetIn overloads
SetIn(TimeSpan)
Creates a deadline that expires a specified time after the call, on the system clock.
Parameters
durationTimeSpan- The time from the call to the instant of the deadline, or
Timeout.InfiniteTimeSpanfor a deadline that never expires.
Returns
Deadline A deadline on TimeProvider.System, or Deadline.Eternal if duration is Timeout.InfiniteTimeSpan.
Exceptions
ArgumentOutOfRangeExceptiondurationis negative, and is notTimeout.InfiniteTimeSpan.
Remarks
This method follows the rules of the BCL for a timeout. If duration is TimeSpan.Zero, the deadline has expired.
If the clock has no timestamp for the instant, because duration is too long, this method returns Deadline.Eternal.
SetIn(TimeSpan, TimeProvider)
Creates a deadline that expires a specified time after the call, on a specified clock.
public static Deadline SetIn(TimeSpan duration, TimeProvider timeProvider)Parameters
durationTimeSpan- The time from the call to the instant of the deadline, or
Timeout.InfiniteTimeSpanfor a deadline that never expires. timeProviderTimeProvider- The clock of the deadline.
Returns
Deadline A deadline on timeProvider, or Deadline.Eternal if duration is Timeout.InfiniteTimeSpan.
Exceptions
ArgumentNullExceptiontimeProviderisnull.ArgumentException- The
TimeProvider.TimestampFrequencyoftimeProvideris zero or negative. ArgumentOutOfRangeExceptiondurationis negative, and is notTimeout.InfiniteTimeSpan.
Remarks
This method follows the rules of the BCL for a timeout. If duration is TimeSpan.Zero, the deadline has expired.
If timeProvider has no timestamp for the instant, because duration is too long, this method returns Deadline.Eternal.
ThrowIfExpired()
Throws a TimeoutException if this deadline has expired.
public void ThrowIfExpired()Exceptions
TimeoutException- This deadline has expired.
ToString()
Returns a string that describes the state of this deadline.
public override string ToString()Returns
string Eternal if this deadline never expires; Expired if it has expired; otherwise, Deadline.RemainingTime in the constant format of TimeSpan.
WaitAsync(CancellationToken)
Asynchronously waits until this deadline expires.
public Task WaitAsync(CancellationToken cancellationToken = default)Parameters
cancellationTokenCancellationToken- A
CancellationTokenthat, when signaled, ends the wait.
Returns
Task A Task that completes when this deadline expires.
Exceptions
OperationCanceledExceptioncancellationTokenwas signaled before the wait ended.
Remarks
If this deadline has expired, the task is complete when this method returns. If this deadline never expires, the task completes only when cancellationToken is signaled.
This method waits on a timer of the clock, which it sets to a whole number of milliseconds. The task does not complete before this deadline expires. It can complete up to one millisecond later, or more when the timer is late.
If cancellationToken is signaled when this method is called, the task is canceled, even if this deadline has expired.
Operators
| Name | Description |
|---|---|
| operator ==(Deadline, Deadline) | Determines whether two specified deadlines are equal. |
| operator !=(Deadline, Deadline) | Determines whether two specified deadlines are different. |