Corio
Corio

Reputation: 395

ManualResetEventSlim.Wait() - timeout in TimeSpan vs int

You can use either ManualResetEventSlim.Wait(TimeSpan timeout) or ManualResetEventSlim.Wait(int millisecondsTimeout).

But int.MaxValue(2,147,483,647 - approx 24.85 days) is much less than TimeSpan.MaxValue.TotalMilliseconds (922,337,203,685,477 - approx 10,675,199.11 days).

I suspect that there is a difference between usages of those overloads.

Upvotes: 0

Views: 529

Answers (1)

Corio
Corio

Reputation: 395

According to Reference Source for mscorlib (.NET Framework 4.7.1):

public bool Wait(TimeSpan timeout)
{
    long totalMilliseconds = (long)timeout.TotalMilliseconds;
    if (totalMilliseconds < -1 || totalMilliseconds > int.MaxValue)
    {
        throw new ArgumentOutOfRangeException("timeout");
    }

    return Wait((int)totalMilliseconds, new CancellationToken());
}

Wait(TimeSpan timeout) checks TotalMilliseconds to be in bounds of positive int value and then calls Wait(int millisecondsTimeout).

So anyway the max wait value will be int.MaxValue. This is also true for all WaitHandle inheritors.

Upvotes: 0

Related Questions