Skip to content

Latest commit

 

History

History
121 lines (67 loc) · 7.49 KB

how-to-complete-asynchronous-operations-using-wrl.md

File metadata and controls

121 lines (67 loc) · 7.49 KB
descriptiontitlems.datems.topicms.assetid
Learn more about: How to: Complete Asynchronous Operations Using WRL
How to: Complete Asynchronous Operations Using WRL
11/04/2016
reference
02173eae-731b-49bc-b412-f1f69388b99d

How to: Complete Asynchronous Operations Using WRL

This document shows how to use the Windows Runtime C++ Template Library (WRL) to start asynchronous operations and perform work when the operations complete.

This document shows two examples. The first example starts an asynchronous timer and waits for the timer to expire. In this example, you specify the asynchronous action when you create the timer object. The second example runs a background worker thread. This example shows how to work with a Windows Runtime method that returns an IAsyncInfo interface. The Callback function is an important part of both examples because it enables them to specify an event handler to process the results of the asynchronous operations.

For a more basic example that creates an instance of a component and retrieves a property value, see How to: Activate and Use a Windows Runtime Component.

Tip

These examples use lambda expressions to define the callbacks. You can also use function objects (functors), function pointers, or std::function objects. For more information about C++ lambda expressions, see Lambda Expressions.

Example: Working with a Timer

The following steps start an asynchronous timer and wait for the timer to expire. The complete example follows.

Warning

Although you typically use the Windows Runtime C++ Template Library in a Universal Windows Platform (UWP) app, this example uses a console app for illustration. Functions such as wprintf_s are not available from a UWP app. For more information about the types and functions that you can use in a UWP app, see CRT functions not supported in Universal Windows Platform apps and Win32 and COM for UWP apps.

  1. Include (#include) any required Windows Runtime, Windows Runtime C++ Template Library, or C++ Standard Library headers.

    [!code-cppwrl-consume-async#2]

    Windows.System.Threading.h declares the types that are required to use an asynchronous timer.

    We recommend that you utilize the using namespace directive in your .cpp file to make the code more readable.

  2. Initialize the Windows Runtime.

    [!code-cppwrl-consume-async#3]

  3. Create an activation factory for the ABI::Windows::System::Threading::IThreadPoolTimer interface.

    [!code-cppwrl-consume-async#4]

    The Windows Runtime uses fully-qualified names to identify types. The RuntimeClass_Windows_System_Threading_ThreadPoolTimer parameter is a string that's provided by the Windows Runtime and contains the required runtime class name.

  4. Create an Event object that synchronizes the timer callback to the main app.

    [!code-cppwrl-consume-async#5]

    [!NOTE] This event is for demonstration only as part of a console app. This example uses the event to ensure that an async operation completes before the app exits. In most apps, you typically don't wait for async operations to complete.

  5. Create an IThreadPoolTimer object that expires after two seconds. Use the Callback function to create the event handler (an ABI::Windows::System::Threading::ITimerElapsedHandler object).

    [!code-cppwrl-consume-async#6]

  6. Print a message to the console and wait for the timer callback to complete. All ComPtr and RAII objects leave scope and are released automatically.

    [!code-cppwrl-consume-async#7]

Here is the complete example:

[!code-cppwrl-consume-async#1]

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 wrl-consume-async.cpp and then run the following command in a Visual Studio Command Prompt window.

cl.exe wrl-consume-async.cpp runtimeobject.lib

Example: Working with a Background Thread

The following steps start a worker thread and define the action that's performed by that thread. The complete example follows.

Tip

This example demonstrates how to work with the ABI::Windows::Foundation::IAsyncAction interface. You can apply this pattern to any interface that implements IAsyncInfo: IAsyncAction, IAsyncActionWithProgress, IAsyncOperation, and IAsyncOperationWithProgress.

  1. Include (#include) any required Windows Runtime, Windows Runtime C++ Template Library, or C++ Standard Library headers.

    [!code-cppwrl-consume-asyncOp#2]

    Windows.System.Threading.h declares the types that are required to use a worker thread.

    We recommend that you use the using namespace directive in your .cpp file to make the code more readable.

  2. Initialize the Windows Runtime.

    [!code-cppwrl-consume-asyncOp#3]

  3. Create an activation factory for the ABI::Windows::System::Threading::IThreadPoolStatics interface.

    [!code-cppwrl-consume-asyncOp#4]

  4. Create an Event object that synchronizes completion of the worker thread to the main app.

    [!code-cppwrl-consume-asyncOp#5]

    [!NOTE] This event is for demonstration only as part of a console app. This example uses the event to ensure that an async operation completes before the app exits. In most apps, you typically don't wait for async operations to complete.

  5. Call the IThreadPoolStatics::RunAsync method to create a worker thread. Use the Callback function to define the action.

    [!code-cppwrl-consume-asyncOp#6]

    The IsPrime function is defined in the complete example that follows.

  6. Print a message to the console and wait for the thread to complete. All ComPtr and RAII objects leave scope and are released automatically.

    [!code-cppwrl-consume-asyncOp#7]

Here is the complete example:

[!code-cppwrl-consume-asyncOp#1]

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 wrl-consume-asyncOp.cpp and then run the following command in a Visual Studio Command Prompt window.

cl.exe wrl-consume-asyncOp.cpp runtimeobject.lib

See also

Windows Runtime C++ Template Library (WRL)

close