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

NameDescription
EternalGets a deadline that never expires.
ExpiredGets a deadline that has expired.
HasExpiredGets a value indicating whether this deadline has expired.
IsEternalGets a value indicating whether this deadline never expires.
RemainingTimeGets the time that remains until this deadline expires.
TimeProviderGets the clock of this deadline.

Eternal

Gets a deadline that never expires.

public static Deadline Eternal { get; }

Property value

Deadline A deadline that never expires, which is also the default value of Deadline.

Expired

Gets a deadline that has expired.

public static Deadline Expired { get; }

Property value

Deadline A deadline that has expired, and depends on no clock.

HasExpired

Gets a value indicating whether this deadline has expired.

public bool HasExpired { get; }

Property value

bool true if the clock of this deadline has reached its instant; otherwise, false.

IsEternal

Gets a value indicating whether this deadline never expires.

public bool IsEternal { get; }

Property value

bool true if this deadline never expires; otherwise, false.

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

NameDescription
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.

public static int Compare(Deadline first, Deadline second)

Parameters

first Deadline
The first deadline to compare.
second Deadline
The second deadline to compare.

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.

public static Deadline EarliestOf(Deadline first, Deadline second)
Parameters
first Deadline
The first deadline to compare.
second Deadline
The second deadline to compare.
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.

public static Deadline EarliestOf(Deadline first, Deadline second, Deadline third)
Parameters
first Deadline
The first deadline to compare.
second Deadline
The second deadline to compare.
third Deadline
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.

public static Deadline EarliestOf(Deadline first, Deadline second, Deadline third, Deadline fourth)
Parameters
first Deadline
The first deadline to compare.
second Deadline
The second deadline to compare.
third Deadline
The third deadline to compare.
fourth Deadline
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
deadlines IEnumerable<Deadline>
The deadlines to compare.
Returns

Deadline The deadline in deadlines that expires first, or Deadline.Eternal if deadlines is empty.

Exceptions
ArgumentNullException
deadlines is null.
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)

Determines whether this deadline and a specified deadline are equal.

public bool Equals(Deadline other)
Parameters
other Deadline
The deadline to compare with this deadline.
Returns

bool true if the two deadlines have the same clock and the same instant; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)
Parameters
obj object?
The object to compare with the current instance.
Returns

bool true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int A 32-bit signed integer that is the hash code for this instance.

LatestOf overloads

LatestOf(Deadline, Deadline)

Returns the one of two deadlines that expires last.

public static Deadline LatestOf(Deadline first, Deadline second)
Parameters
first Deadline
The first deadline to compare.
second Deadline
The second deadline to compare.
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.

public static Deadline LatestOf(Deadline first, Deadline second, Deadline third)
Parameters
first Deadline
The first deadline to compare.
second Deadline
The second deadline to compare.
third Deadline
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.

public static Deadline LatestOf(Deadline first, Deadline second, Deadline third, Deadline fourth)
Parameters
first Deadline
The first deadline to compare.
second Deadline
The second deadline to compare.
third Deadline
The third deadline to compare.
fourth Deadline
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
deadlines IEnumerable<Deadline>
The deadlines to compare.
Returns

Deadline The deadline in deadlines that expires last, or Deadline.Expired if deadlines is empty.

Exceptions
ArgumentNullException
deadlines is null.
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.

public static Deadline SetAfter(Deadline deadline, TimeSpan duration)

Parameters

deadline Deadline
The deadline that the new deadline follows.
duration TimeSpan
The time from the instant of deadline to the instant of the new deadline, or Timeout.InfiniteTimeSpan for 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

ArgumentOutOfRangeException
duration is negative, and is not Timeout.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
instant DateTimeOffset
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
instant DateTimeOffset
The date and time at which the deadline expires.
timeProvider TimeProvider
The clock of the deadline.
Returns

Deadline A deadline on timeProvider.

Exceptions
ArgumentNullException
timeProvider is null.
ArgumentException
The TimeProvider.TimestampFrequency of timeProvider is 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.

public static Deadline SetBefore(Deadline deadline, TimeSpan duration)

Parameters

deadline Deadline
The deadline that the new deadline precedes.
duration TimeSpan
The time from the instant of the new deadline to the instant of deadline, or Timeout.InfiniteTimeSpan for 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

ArgumentOutOfRangeException
duration is negative, and is not Timeout.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.

public static Deadline SetIn(TimeSpan duration)
Parameters
duration TimeSpan
The time from the call to the instant of the deadline, or Timeout.InfiniteTimeSpan for a deadline that never expires.
Returns

Deadline A deadline on TimeProvider.System, or Deadline.Eternal if duration is Timeout.InfiniteTimeSpan.

Exceptions
ArgumentOutOfRangeException
duration is negative, and is not Timeout.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
duration TimeSpan
The time from the call to the instant of the deadline, or Timeout.InfiniteTimeSpan for a deadline that never expires.
timeProvider TimeProvider
The clock of the deadline.
Returns

Deadline A deadline on timeProvider, or Deadline.Eternal if duration is Timeout.InfiniteTimeSpan.

Exceptions
ArgumentNullException
timeProvider is null.
ArgumentException
The TimeProvider.TimestampFrequency of timeProvider is zero or negative.
ArgumentOutOfRangeException
duration is negative, and is not Timeout.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

cancellationToken CancellationToken
A CancellationToken that, when signaled, ends the wait.

Returns

Task A Task that completes when this deadline expires.

Exceptions

OperationCanceledException
cancellationToken was 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

NameDescription
operator ==(Deadline, Deadline)Determines whether two specified deadlines are equal.
operator !=(Deadline, Deadline)Determines whether two specified deadlines are different.

operator ==(Deadline, Deadline)

Determines whether two specified deadlines are equal.

public static bool operator ==(Deadline a, Deadline b)

Parameters

a Deadline
The first deadline to compare.
b Deadline
The second deadline to compare.

Returns

bool true if a and b have the same clock and the same instant; otherwise, false.

operator !=(Deadline, Deadline)

Determines whether two specified deadlines are different.

public static bool operator !=(Deadline a, Deadline b)

Parameters

a Deadline
The first deadline to compare.
b Deadline
The second deadline to compare.

Returns

bool true if a and b differ in their clock or in their instant; otherwise, false.