| description | Learn more about: How to: Handle Events Using WRL |
|---|---|
| title | How to: Handle Events Using WRL |
| ms.date | 11/04/2016 |
| ms.topic | reference |
| ms.assetid | 1c77543f-7b0c-4a94-93bf-e3225885ed76 |
This document shows how to use the Windows Runtime C++ Template Library (WRL)to subscribe to and handle the events of a Windows Runtime object.
For a more basic example that creates an instance of that component and retrieves a property value, see How to: Activate and Use a Windows Runtime Component.
The following steps start an ABI::Windows::System::Threading::IDeviceWatcher object and use event handlers to monitor progress. The IDeviceWatcher interface enables you to enumerate devices asynchronously, or in the background, and receive notification when devices are added, removed, or changed. The Callback function is an important part of this example because it enables it to specify event handlers that process the results of the background operation. The complete example follows.
Warning
Although you typically use the Windows Runtime C++ Template Library in a Universal Windows Platform app, this example uses a console app for illustration. Functions such as wprintf_s are not available from a Universal Windows Platform app. For more information about the types and functions that you can use in a Universal Windows Platform app, see CRT functions not supported in Universal Windows Platform apps and Win32 and COM for UWP apps.
-
Include (
#include) any required Windows Runtime, Windows Runtime C++ Template Library, or C++ Standard Library headers.[!code-cppwrl-consume-event#2]
Windows.Devices.Enumeration.hdeclares the types that are required to enumerate devices.We recommend that you utilize the
using namespacedirective in your .cpp file to make the code more readable. -
Declare the local variables for the app. This example holds count of the number of enumerated devices and registration tokens that enable it to later unsubscribe from events.
[!code-cppwrl-consume-event#7]
-
Initialize the Windows Runtime.
[!code-cppwrl-consume-event#3]
-
Create an Event object that synchronizes the completion of the enumeration process to the main app.
[!code-cppwrl-consume-event#4]
[!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.
-
Create an activation factory for the
IDeviceWatcherinterface.[!code-cppwrl-consume-event#5]
The Windows Runtime uses fully-qualified names to identify types. The
RuntimeClass_Windows_Devices_Enumeration_DeviceInformationparameter is a string that's provided by the Windows Runtime and contains the required runtime class name. -
Create the
IDeviceWatcherobject.[!code-cppwrl-consume-event#6]
-
Use the
Callbackfunction to subscribe to theAdded,EnumerationCompleted, andStoppedevents.[!code-cppwrl-consume-event#8]
The
Addedevent handler increments the count of enumerated devices. It stops the enumeration process after ten devices are found.The
Stoppedevent handler removes the event handlers and sets the completion event.The
EnumerationCompletedevent handler stops the enumeration process. We handle this event in case there are fewer than ten devices.[!TIP] This example uses a lambda expression to define the callbacks. You can also use function objects (functors), function pointers, or std::function objects. For more information about lambda expressions, see Lambda Expressions.
-
Start the enumeration process.
[!code-cppwrl-consume-event#9]
-
Wait for the enumeration process to complete and then print a message. All
ComPtrand RAII objects leave scope and are released automatically.[!code-cppwrl-consume-event#10]
Here is the complete example:
[!code-cppwrl-consume-event#1]
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-events.cpp and then run the following command in a Visual Studio Command Prompt window.
cl.exe wrl-consume-events.cpp runtimeobject.lib