Alex Rivera | Logout

How to Schedule a task for future execution in Task Parallel Library

Asked 2010-11-19T22:24:19.127
12

Is there a way to schedule a Task for execution in the future using the Task Parallel Library?

I realize I could do this with pre-.NET4 methods such as System.Threading.Timer ... however if there is a TPL way to do this I'd rather stay within the design of the framework. I am not able to find one however.

Thank you.

Edit
Report

1 Answer

2

Set a one-shot timer that, when fired, starts the task. For example, the code below will wait five minutes before starting the task.

TimeSpan TimeToWait = TimeSpan.FromMinutes(5);
Timer t = new Timer((s) =>
    {
        // start the task here
    }, null, TimeToWait, TimeSpan.FromMilliseconds(-1));

The TimeSpan.FromMilliseconds(-1) makes the timer a one-shot rather than a periodic timer.

answered 2010-11-19T22:58:23.623

Your Answer