-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Introduce TimeProvider extension method to create a CTS to be cancelled after a specified time period. #85080
Comments
Tagging subscribers to this area: @mangod9 Issue DetailsBackground and motivationA new constructor called private static void CancelAfter(TimeProvider provider, CancellationTokenSource cts, TimeSpan delay)
{
if (provider == TimeProvider.System)
{
cts.CancelAfter(delay);
}
else
{
ITimer timer = provider.CreateTimer(s => ((CancellationTokenSource)s).Cancel(), cts, delay, Timeout.InfiniteTimeSpan);
cts.Token.Register(t => ((ITimer)t).Dispose(), timer);
}
} The experience would be unsatisfactory when users want to write a ns2.0 library code that works in all supported framework versions. API Proposalnamespace System.Threading.Tasks
{
public static class TimeProviderTaskExtensions
{
/// <summary>Initializes a new instance of the <see cref="CancellationTokenSource"/> class that will be canceled after the specified <see cref="TimeSpan"/>. </summary>
/// <param name="timeProvider">The <see cref="TimeProvider"/> with which to interpret the <paramref name="delay"/>. </param>
/// <param name="delay">The time interval to wait before canceling this <see cref="CancellationTokenSource"/>. </param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="delay"/>'s <see cref="TimeSpan.TotalMilliseconds"/> is less than -1 or greater than <see cref="uint.MaxValue"/> - 1. </exception>
/// <remarks>
/// The countdown for the delay starts during the call to the constructor. When the delay expires,
/// the constructed <see cref="CancellationTokenSource"/> is canceled if it has
/// not been canceled already. Subsequent calls to CancelAfter will reset the delay for the constructed
/// <see cref="CancellationTokenSource"/> if it has not been canceled already.
/// </remarks>
public static CancellationTokenSource CreateCancellationTokenSource(this TimeProvider timeProvider, TimeSpan delay) ;
}
} API UsageCancellationTokenSource cts = TimeProvider.System.CreateCancellationTokenSource(TimeSpan.FromMilliseconds(100));
....
cts.Task.WaitAsync(CancellationToken.None); Alternative DesignsNo response RisksNo response
|
Tagging subscribers to this area: @dotnet/area-system-datetime Issue DetailsBackground and motivationA new constructor called private static void CancelAfter(TimeProvider provider, CancellationTokenSource cts, TimeSpan delay)
{
if (provider == TimeProvider.System)
{
cts.CancelAfter(delay);
}
else
{
ITimer timer = provider.CreateTimer(s => ((CancellationTokenSource)s).Cancel(), cts, delay, Timeout.InfiniteTimeSpan);
cts.Token.Register(t => ((ITimer)t).Dispose(), timer);
}
} The experience would be unsatisfactory when users want to write a ns2.0 library code that works in all supported framework versions. API Proposalnamespace System.Threading.Tasks
{
public static class TimeProviderTaskExtensions
{
/// <summary>Initializes a new instance of the <see cref="CancellationTokenSource"/> class that will be canceled after the specified <see cref="TimeSpan"/>. </summary>
/// <param name="timeProvider">The <see cref="TimeProvider"/> with which to interpret the <paramref name="delay"/>. </param>
/// <param name="delay">The time interval to wait before canceling this <see cref="CancellationTokenSource"/>. </param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="delay"/>'s <see cref="TimeSpan.TotalMilliseconds"/> is less than -1 or greater than <see cref="uint.MaxValue"/> - 1. </exception>
/// <remarks>
/// The countdown for the delay starts during the call to the constructor. When the delay expires,
/// the constructed <see cref="CancellationTokenSource"/> is canceled if it has
/// not been canceled already. Subsequent calls to CancelAfter will reset the delay for the constructed
/// <see cref="CancellationTokenSource"/> if it has not been canceled already.
/// </remarks>
public static CancellationTokenSource CreateCancellationTokenSource(this TimeProvider timeProvider, TimeSpan delay) ;
}
} API UsageCancellationTokenSource cts = TimeProvider.System.CreateCancellationTokenSource(TimeSpan.FromMilliseconds(100));
....
cts.Task.WaitAsync(CancellationToken.None); Alternative DesignsNo response RisksNo response
|
On the basis that this is only provided in the namespace System.Threading.Tasks
{
public static partial class TimeProviderTaskExtensions
{
/// <summary>Initializes a new instance of the <see cref="CancellationTokenSource"/> class that will be canceled after the specified <see cref="TimeSpan"/>. </summary>
/// <param name="timeProvider">The <see cref="TimeProvider"/> with which to interpret the <paramref name="delay"/>. </param>
/// <param name="delay">The time interval to wait before canceling this <see cref="CancellationTokenSource"/>. </param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="delay"/>'s <see cref="TimeSpan.TotalMilliseconds"/> is less than -1 or greater than <see cref="uint.MaxValue"/> - 1. </exception>
/// <remarks>
/// The countdown for the delay starts during the call to the constructor. When the delay expires,
/// the constructed <see cref="CancellationTokenSource"/> is canceled if it has
/// not been canceled already. Subsequent calls to CancelAfter will reset the delay for the constructed
/// <see cref="CancellationTokenSource"/> if it has not been canceled already.
/// </remarks>
public static CancellationTokenSource CreateCancellationTokenSource(this TimeProvider timeProvider, TimeSpan delay) ;
}
} |
@tarekgh I know this is closed, but wonder if you considered the following extension method as an alternative? /// <summary>
/// Schedules a Cancel operation on the <paramref name="cancellationTokenSource"/>.
/// </summary>
/// <param name="cancellationTokenSource">
/// The <see cref="CancellationTokenSource"/> to cancel after the specified delay.
/// </param>
/// <param name="delay">The time span to wait before canceling the <paramref name="cancellationTokenSource"/>.
/// </param>
/// <param name="timeProvider">
/// The <see cref="TimeProvider"/> to use for scheduling the cancellation.
/// </param>
/// <exception cref="ObjectDisposedException">The exception thrown when the <paramref name="cancellationTokenSource"/>
/// has been disposed.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// The <paramref name="delay"/> is less than -1 or greater than maximum allowed timer duration.
/// </exception>
/// <exception cref="ArgumentNullException">The <paramref name="cancellationTokenSource"/> is null.</exception>
/// <remarks>
/// <para>
/// The countdown for the delay starts during this call. When the delay expires,
/// the <paramref name="cancellationTokenSource"/> is canceled, if it has
/// not been canceled already.
/// </para>
/// <para>
/// Subsequent calls to CancelAfter will reset the delay for the
/// <paramref name="cancellationTokenSource"/>, if it has not been canceled already.
/// </para>
/// </remarks>
public static void CancelAfter(this CancellationTokenSource cancellationTokenSource, TimeSpan delay, TimeProvider timeProvider); |
@egil you may review the detailed comment #36617 (comment) why we are trying to avoid CancelAfter. |
Thanks for the link. |
Background and motivation
A new constructor called
CancellationTokenSource(TimeSpan, TimeProvider)
has been introduced for creating a CTS object that automatically gets cancelled after a specified period using the TimeProvider. However, this constructor is only available on .NET 8.0. For users who are using TimeProvider on down-level versions, they will need to write their own code to achieve the same functionality. An example of such code is given below.The experience would be unsatisfactory when users want to write a ns2.0 library code that works in all supported framework versions.
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: