计划任务以供将来执行 [英] Scheduling Task for future execution
问题描述
我查看了计时器类API,但找不到有关如何安排Task
以便将来执行的信息.
使用Timer
类,我可以安排线程以供将来执行,但是我需要安排Task
.
Task
具有.Delay(...)
方法,但是不确定延迟是否类似于调度.
I have looked at the Task and Timer class API's, but could not find information on how to schedule a Task
for future execution.
Using the Timer
class, I can schedule threads for future execution, but I need to schedule Task
s.
Task
has .Delay(...)
methods, but not sure delay is similar to scheduling.
编辑(说明):我想在x分钟后开始执行任务.
Edit(clarification): I want to start tasks after x minutes.
推荐答案
您应使用Task.Delay
(内部使用System.Threading.Timer
实现):
You should use Task.Delay
(which internally is implemented using a System.Threading.Timer
):
async Task Foo()
{
await Task.Delay(TimeSpan.FromMinutes(30));
// Do something
}
虽然执行了延迟" 没有线程被使用.当等待结束后,工作将按计划执行.
While the delay is "executed" there is no thread being used. When the wait ends the work after it would be scheduled for execution.
这篇关于计划任务以供将来执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!