Skip to content

Latest commit

 

History

History
53 lines (35 loc) · 4.88 KB

how-to-create-a-task-that-completes-after-a-delay.md

File metadata and controls

53 lines (35 loc) · 4.88 KB
title description ms.date helpviewer_keywords ms.assetid
How to: Create a task that completes after a delay
Create a task that completes after a delay by using the PPL ConcRT library.
10/19/2020
task_completion_event class, example
create a task that completes after a delay, example [C++]
3fc0a194-3fdb-4eba-8b8a-b890981a985d

How to: Create a task that completes after a delay

This example shows how to use the concurrency::task, concurrency::cancellation_token_source, concurrency::cancellation_token, concurrency::task_completion_event, concurrency::timer, and concurrency::call classes to create a task that completes after a delay. You can use this method to build loops that occasionally poll for data. You can also introduce timeouts, delay handling of user input for a predetermined time, and so on.

Example: complete_after and cancel_after_timeout functions

The following example shows the complete_after and cancel_after_timeout functions. The complete_after function creates a task object that completes after the specified delay. It uses a timer object and a call object to set a task_completion_event object after the specified delay. By using the task_completion_event class, you can define a task that completes after a thread or another task signals that a value is available. When the event is set, listener tasks complete and their continuations are scheduled to run.

Tip

For more information about the timer and call classes, which are part of the Asynchronous Agents Library, see Asynchronous Message Blocks.

The cancel_after_timeout function builds on the complete_after function to cancel a task if that task doesn't complete before a given timeout. The cancel_after_timeout function creates two tasks. The first task indicates success and completes after the provided task completes. The second task indicates failure and completes after the specified timeout. The cancel_after_timeout function creates a continuation task that runs when the success or failure task completes. If the failure task completes first, the continuation cancels the token source to cancel the overall task.

[!code-cppconcrt-task-delay#1]

Example: Compute count of prime numbers

The following example computes the count of prime numbers in the range [0, 100000] multiple times. The operation fails if it doesn't complete in a given time limit. The count_primes function demonstrates how to use the cancel_after_timeout function. It counts the number of primes in the given range and fails if the task doesn't complete in the provided time. The wmain function calls the count_primes function multiple times. Each time, it halves the time limit. The program finishes after the operation doesn't complete in the current time limit.

[!code-cppconcrt-task-delay#2]

When you use this technique to cancel tasks after a delay, any unstarted tasks won't start after the overall task is canceled. However, it's important for any long-running tasks to respond to cancellation quickly. For more information about task cancellation, see Cancellation in the PPL.

Complete code example

Here is the complete code for this example:

[!code-cppconcrt-task-delay#3]

Compiling the Code

To compile the code, copy it and then paste it in a Visual Studio project, or paste it in a file that is named task-delay.cpp and then run the following command in a Visual Studio Command Prompt window.

cl.exe /EHsc task-delay.cpp

See also

Task Parallelism
task Class (Concurrency Runtime)
cancellation_token_source Class
cancellation_token Class
task_completion_event Class
timer Class
call Class
Asynchronous Message Blocks
Cancellation in the PPL