English 中文(简体)
为什么任务。 拖 () 允许无限延迟?
原标题:Why does Task.Delay() allow an infinite delay?

在我应用程序冻结后,我追踪到一个线索的原因,等待 Task. Delay () (或 TaskEx. Delay () in.NET 4.0) 所创建的任务,该任务提供了计算出来的 TimeSpan ,由于一个错误,有时被计算为 TimeSpan ,其 TotalMillicons 小于或等于 -1 或大于 -2 (即从-100到-19999秒之间任何地方计算在内)。

当您通过负 TimeSpan ,即 -2 毫秒或更低时,该方法会正确地丢出一个 说明排除排除的参数 ,但当您提供上述范围的负时间span时,它会返回一个从未完成的 Task (通过设置 System.Treading.Timer < dueTime > -1,其中注明无限性),这意味着该任务上设置的任何延续将永远无法执行,而发生在 .wait() 上的任何差线将永远被屏蔽。

从未完成的 < code> Task 是否有可能有用? 有人会期待这样的返回值吗? 是否应该将负值传递到 < code> 。 Delay() , 包括该特殊范围内的数值, 丢出一个 < code > 演示排除 ?

最佳回答

"http://msdn.microsoft.com/ en- us/library/ system.threading.taimout.infinite.aspx" >Timeout.Infinite 或 -1 在您想要无限期地等待一项长期任务时有用, 长期任务需要一定的时间完成, 但最终会完成 。

Win32 API 也使用恒定 IninlinITE = -1 用于无限超时 。

您通常不会想要在 UI 线索中使用它, 因为它可以冻结 UI( 这似乎是你的问题 ) 。 但工人线索中存在有效的使用案例, 比如一个服务器, 它正在屏蔽等待客户端的连接 。

问题回答

I m using it when I want console application not to terminate. For example, when I m writing a Web Job for Azure, I need a program that never terminates. Here s the template:

public async Task Main(string args[])
{
    var timer = new Timer();
    timer.Interval = 6000;
    timer.Elapsed += (sender, args) =>
    {
        // Do something repeatedly, each 1 minute.
    };
    timer.Start();

    // Now what to do to stop the application from terminating!?
    // That s the magic:
    await Task.Delay(-1);
}

在模拟假想时, 我想要确保我在一个任务中的代码。 当任何( ) 区块正正确处理等待的任务之一时, 我可能会嘲笑其他任务, 并使用无限的延迟来确保任务。 当有人正在处理我的任务时, 我并没有把任务当作无限的延迟 。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签