Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 284
Slaggg Avatar asked Nov 19 '10 22:11

Slaggg


People also ask

What is a continuation Task?

A continuation task (also known just as a continuation) is an asynchronous task that's invoked by another task, known as the antecedent, when the antecedent finishes.

What is task Parallel Library in c#?

The Task Parallel Library (TPL) is a set of public types and APIs in the System. Threading and System. Threading. Tasks namespaces. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications.

How to create a new Task c#?

To start a task in C#, follow any of the below given ways. Use a delegate to start a task. Task t = new Task(delegate { PrintMessage(); }); t. Start();

Which method provides a Convenient way to run any number of arbitrary statements concurrently?

Invoke method provides a convenient way to run any number of arbitrary statements concurrently. Just pass in an Action delegate for each item of work. The easiest way to create these delegates is to use lambda expressions. The lambda expression can either call a named method or provide the code inline.


1 Answers

This feature was introduced in the Async CTP, which has now been rolled into .NET 4.5. Doing it as follows does not block the thread, but returns a Task which will execute in the future.

Task<MyType> new_task = Task.Delay(TimeSpan.FromMinutes(5))
                            .ContinueWith<MyType>( /*...*/ );

(If using the old Async releases, use the static class TaskEx instead of Task)

like image 99
Glenn Slayden Avatar answered Oct 18 '22 03:10

Glenn Slayden