Walkthrough: Adapting Existing Code to Use Lightweight Tasks

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

This topic shows how to adapt existing code that uses the Windows API to create and execute a thread to use a lightweight task.

A lightweight task is a task that you schedule directly from a concurrency::Scheduler or concurrency::ScheduleGroup object. Lightweight tasks are useful when you adapt existing code to use the scheduling functionality of the Concurrency Runtime.

Prerequisites

Before you start this walkthrough, read the topic Task Scheduler.

Example

Description

The following example illustrates typical usage of the Windows API to create and execute a thread. This example uses the CreateThread function to call the MyThreadFunction on a separate thread.

Code

No code example is currently available or this language may not be supported.

Comments

This example produces the following output.

Parameters = 50, 100  

The following steps show how to adapt the code example to use the Concurrency Runtime to perform the same task.

To adapt the example to use a lightweight task

  1. Add a #include directive for the header file concrt.h.

No code example is currently available or this language may not be supported.

  1. Add a using directive for the concurrency namespace.

No code example is currently available or this language may not be supported.

  1. Change the declaration of MyThreadFunction to use the __cdecl calling convention and to return void.

No code example is currently available or this language may not be supported.

  1. Modify the MyData structure to include a concurrency::event object that signals to the main application that the task has finished.

No code example is currently available or this language may not be supported.

  1. Replace the call to CreateThread with a call to the concurrency::CurrentScheduler::ScheduleTask method.

No code example is currently available or this language may not be supported.

  1. Replace the call to WaitForSingleObject with a call to the concurrency::event::wait method to wait for the task to finish.

No code example is currently available or this language may not be supported.

  1. Remove the call to CloseHandle.

  2. Change the signature of the definition of MyThreadFunction to match step 3.

No code example is currently available or this language may not be supported.

  1. At the end of the MyThreadFunction function, call the concurrency::event::set method to signal to the main application that the task has finished.

No code example is currently available or this language may not be supported.

  1. Remove the return statement from MyThreadFunction.
Example

Description

The following completed example shows code that uses a lightweight task to call the MyThreadFunction function.

Code

No code example is currently available or this language may not be supported.

Comments

See Also

Task Scheduler
Scheduler Class

Related help topics
Loading...
No resources were found.