DeviceWatcher Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Enumerates devices dynamically, so that the app receives notifications if devices are added, removed, or changed after the initial enumeration is complete.
public ref class DeviceWatcher sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class DeviceWatcher final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class DeviceWatcher
Public NotInheritable Class DeviceWatcher
- Inheritance
- Attributes
Device family |
Windows 10 (introduced in 10.0.10240.0)
|
API contract |
Windows.Foundation.UniversalApiContract (introduced in v1.0)
|
For complete examples in C# and C++, see the Device enumeration and pairing sample.
An app calls Start to begin the search for devices. During this initial enumeration, the DeviceWatcher raises an Added event for each device that's found, until all devices are found. The DeviceWatcher raises an EnumerationCompleted event when the initial enumeration is complete, and continues to raise events if a device is added, updated, or removed.
The following diagram shows how the DeviceWatcher transitions between the states represented by DeviceWatcherStatus enumeration.
The Start method can only be called when the DeviceWatcher is in the Created, Stopped or Aborted state. The Status property indicates the DeviceWatcher state. When re-starting the watcher, wait for the Stopped event before calling Start.
Stop transitions the DeviceWatcher to the Stopping state and completes immediately. The watcher will transition to the Stopped state once all events that are already in the process of being raised have completed.
Apps may wait for the Stopped event if they need to know when the DeviceWatcher has stopped. Callers must wait for the Stopped event before they can call Start to restart the watcher. Callers may unsubscribe from events if they do not want to receive any additional events after Stop but do not want to wait for the Stopped event.
Note
An app must subscribe to all of the added, removed, and updated events to be notified when there are device additions, removals or updates. If an app handles only the added event, it will not receive an update if a device is added to the system after the initial device enumeration completes.
Status |
The status of the DeviceWatcher. |
Get |
Gets a DeviceWatcherTrigger object monitoring for changes to the list of devices. |
Start() |
Starts a search for devices, and subscribes to device enumeration events. |
Stop() |
Stop raising the events that add, update and remove enumeration results. |
Added |
Event that is raised when a device is added to the collection enumerated by the DeviceWatcher. |
Enumeration |
Event that is raised when the enumeration of devices completes. |
Removed |
Event that is raised when a device is removed from the collection of enumerated devices. |
Stopped |
Event that is raised when the enumeration operation has been stopped. |
Updated |
Event that is raised when a device is updated in the collection of enumerated devices. |
Product | Versions |
---|---|
WinRT | Build 10240, Build 10586, Build 14383, Build 15063, Build 16299, Build 17134, Build 17763, Build 18362, Build 19041, Build 20348, Build 22000, Build 22621, Build 26100 |