Threading Threading
Threading Threading
DocFX + Singulink = ♥

Search Results for

    Struct RetryTimer

    Provides the waits between attempts of an operation that is retried after a delay until a time limit passes, with time measured by a TimeProvider.

    Namespace: Singulink.Threading
    Assembly: Singulink.Threading.dll
    Syntax
    public readonly struct RetryTimer
    Remarks

    The caller makes an attempt, decides whether a failure is worth another try, and if it is, calls TryWait(CancellationToken) or TryWaitAsync(CancellationToken). These wait for the delay and return true while the time limit has not passed, and return false without waiting once it has. The timer starts when it is created with Start(TimeSpan, TimeSpan):

    var retry = RetryTimer.Start(TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(50));
    
    while (!TryMoveFile(source, destination))
    {
        if (!await retry.TryWaitAsync(cancellationToken))
            return false;
    }
    
    return true;

    The last wait is cut short so that it ends when the time limit passes, so a final attempt is made at the limit. Waits are rounded up to whole milliseconds, the resolution of system timers, so that a wait is never cut to nothing and the attempts never run without a pause. A time limit of zero allows one attempt and no retries.

    Code that has to decide whether to give up separately from the wait, for example while it holds a lock that must not be held across the wait, can check CanRetry and then call Wait(CancellationToken) or WaitAsync(CancellationToken).

    The timer is a small immutable struct: starting it and checking it allocate nothing, so an operation that succeeds on its first attempt costs no allocation. Copies of the timer share its start time.

    Properties

    Name Description
    CanRetry

    Gets a value indicating whether the time limit has not yet passed, so another attempt can be made after a wait.

    Elapsed

    Gets the time that has passed since the timer was started.

    IsDefault

    Gets a value indicating whether the instance is in its default, uninitialized state.

    Methods

    Name Description
    Start(TimeSpan, TimeSpan)

    Starts a timer that measures time with System.

    Start(TimeSpan, TimeSpan, TimeProvider)

    Starts a timer that measures time with the specified time provider.

    TryWait(CancellationToken)

    Waits before another attempt if the time limit has not passed.

    TryWaitAsync(CancellationToken)

    Asynchronously waits before another attempt if the time limit has not passed.

    Wait(CancellationToken)

    Waits before another attempt.

    WaitAsync(CancellationToken)

    Asynchronously waits before another attempt.

    © Singulink. All rights reserved.