我正在获取TimeSpan
一年,并试图为WaitOne
做EventWaitHandle
,但是遇到了错误,
如何处理这种情况?
System.ArgumentOutOfRangeException:'数字必须为非负数,并且小于或等于Int32.MaxValue或-1。 参数名称:超时'
private static readonly EventWaitHandle eventWait = new AutoResetEvent(false);
static void Main(string[] args)
{
try
{
var span = TimeSpan.FromMilliseconds(Math.Round((DateTime.Now.AddYears(1) - DateTime.Now).TotalMilliseconds, 3));
if (eventWait.WaitOne(span))
{
Console.WriteLine("test");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace.ToString());
}
}
答案 0 :(得分:4)
Math.Round((DateTime.Now.AddYears(1) - DateTime.Now).TotalMilliseconds, 3) <= int.MaxValue
是
false
考虑到文档的时间跨度较大,因为int.MaxValue是EventWaitHandle.WaitOne(TimeSpan)消耗无异常的最大时间跨度值。
您可以尝试-1进行无限等待。