gpt4 book ai didi

c# - 线程. sleep (-1)

转载 作者:太空狗 更新时间:2023-10-29 22:10:22 27 4
gpt4 key购买 nike

有什么用

 System.Threading.Thread.Sleep(-1)

我希望这会引发异常,正如 Thread.Sleep 的文档所说

The value of timeout is negative and is not equal to Timeout.Infinite in milliseconds, or is greater than Int32.MaxValue milliseconds.

但是,上面的 Thread.Sleep(-1) 不会抛出异常。当我查看 ReferenceSource 时,我看到了

[System.Security.SecuritySafeCritical]  // auto-generated
public static void Sleep(int millisecondsTimeout)
{
SleepInternal(millisecondsTimeout);
// Ensure we don't return to app code when the pause is underway
if(AppDomainPauseManager.IsPaused)
AppDomainPauseManager.ResumeEvent.WaitOneWithoutFAS();
}

public static void Sleep(TimeSpan timeout)
{
long tm = (long)timeout.TotalMilliseconds;
if (tm < -1 || tm > (long) Int32.MaxValue)
throw new ArgumentOutOfRangeException("timeout",Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
Sleep((int)tm);
}

看起来它不会抛出负时间跨度,而只会抛出小于 -1 的负时间跨度。

确实如此

 Thread.Sleep(-2);

确实会崩溃。

那么这里-1的特例是什么? Thread.Sleep(-1) 实际上在做什么?

最佳答案

Timeout.Infinite 的值== -1。

Timeout.Infinite : A constant used to specify an infinite waiting period, for threading methods that accept an Int32 parameter.

关于c# - 线程. sleep (-1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50269871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com