计划任务以供将来执行 [英] Scheduling Task for future execution

查看:71
本文介绍了计划任务以供将来执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了计时器类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 Tasks. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆