DeviceWatcher DeviceWatcher DeviceWatcher DeviceWatcher Class

Enumerates devices dynamically, so that the app receives notifications if devices are added, removed, or changed after the initial enumeration is complete.

Syntax

Declaration

public sealed class DeviceWatcherpublic sealed class DeviceWatcherPublic NotInheritable Class DeviceWatcher

Remarks

An app calls Start() to begin the search for devices. During this intial 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.

state diagram of DeviceWatcher states

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.

Examples

This example incrementally enumerates devices, adding them to a list each time a device is found, and also handling removals and updates. The onAdded function that handles the Added event takes a DeviceInformation object. Once enumeration is complete, the app prints a list of devices. The app also prints a message if devices are added, updated, or removed after the initial enumeration completes.


<!DOCTYPE html>
<html>
<head>
    <title>Device Enumeration Sample</title>
    <script>
   var watcher;
    var deviceArray = new Array(); // Saves the enumeration results.

    function WatchDevices() {
        try {
            output.innerHTML = ""; // Clears output field.

            watcher = 
                Windows.Devices.Enumeration.DeviceInformation.createWatcher();
            // Add event handlers
            watcher.addEventListener("added", onAdded);
            watcher.addEventListener("removed", onRemoved);
            watcher.addEventListener("updated", onUpdated);
            watcher.addEventListener("enumerationcompleted", 
                onEnumerationCompleted);
            watcher.addEventListener("stopped", onStopped);
            // Start enumerating and listening for events
            watcher.start();
        } catch (e) {
            document.getElementById("statusMessage").innerHTML = 
                "Failed to create watcher, error: " + e.message;
        }
    }

    function stopWatcher() {
        try {
            watcher.stop();
        }
        catch (e) {
            document.getElementById("statusMessage").innerHTML = 
                "Failed to stop watcher: " + e.message;
        }
    }

    function onAdded(devinfo) {
        document.getElementById("output").innerHTML += "<p>Device added: " + 
            devinfo.name + "</p>";
        deviceArray.push(devinfo);
    }

    function onUpdated(devUpdate) {
        document.getElementById("output").innerHTML += "<p>Device updated.</p>";
        for (var i = 0; i < deviceArray.length; i++) {
            if (deviceArray[i].id == devUpdate.id) {
                deviceArray[i].update(devUpdate);
            }
        }
    }

    function onRemoved(devupdate) {
        document.getElementById("output").innerHTML += "<p>Device removed.</p>";
        for (var i = 0; i < deviceArray.length; i++) {
            if (deviceArray[i].id == devupdate.id) {
                deviceArray[i].slice(devupdate);
            }
        }
    }

    function onEnumerationCompleted(obj) {
        document.getElementById("output").innerHTML += 
            "<p>Enumeration Completed.</p>";
        printDeviceArray(document.getElementById("output"));
    }

    function onStopped(obj) {
        document.getElementById("output").innerHTML += "<p>Stopped.</p>";
    }


    // Prints the friendly name of the device interface, 
    // its ID (device interface path), and whether it is enabled.
    function printDevice(deviceInterface, outputDestination) {
        outputDestination.innerHTML += "<p>Name: " + 
            deviceInterface.name + "<p/>"; 
        outputDestination.innerHTML += "<p>Interface ID: " + 
            deviceInterface.id + "<p/>";    
        outputDestination.innerHTML += "<p>Enabled: " + 
            deviceInterface.isEnabled + "<p/>";
        outputDestination.innerHTML += "<br/>";
    }

    function printDeviceArray(outputDestination) {
        for (var i = 0; i < deviceArray.length; i++) {
            printDevice(deviceArray[i], outputDestination);
        }
    }

    </script>
</head>
<body role="application">
    <h1>Device Enumeration Sample</h1>
    <h2 >Input</h2>
    <div >            
            <div >
            <p>This example incrementally enumerates devices, adding them to a list each time a device is found, and also watching for updates.
               Once enumeration is complete, the list of devices is printed.</p> 
                <input type="button" value="Watch(All Devices)" onclick="WatchDevices()"/>
                <br/><br/>

                <input type="button" value="Stop" onclick="stopWatcher()"/>
                <br/><br/>
            </div>
    </div>

    <h2 > Output</h2>
            <div id="statusMessage"></div>
            <!--  Output -->
            <div  id="output"></div>
</body>
</html>

Properties summary

The status of the DeviceWatcher.

Methods summary

Gets a DeviceWatcherTrigger object monitoring for changes to the list of devices.

Starts a search for devices, and subscribes to device enumeration events.

Stop raising the events that add, update and remove enumeration results.

Events summary

Event that is raised when a device is added to the collection enumerated by the DeviceWatcher.

Event that is raised when the enumeration of devices completes.

Event that is raised when a device is removed from the collection of enumerated devices.

Event that is raised when the enumeration operation has been stopped.

Event that is raised when a device is updated in the collection of enumerated devices.

Properties

Methods

  • GetBackgroundTrigger(Windows.Foundation.Collections.IIterable{Windows.Devices.Enumeration.DeviceWatcherEventKind})
    GetBackgroundTrigger(Windows.Foundation.Collections.IIterable{Windows.Devices.Enumeration.DeviceWatcherEventKind})
    GetBackgroundTrigger(Windows.Foundation.Collections.IIterable{Windows.Devices.Enumeration.DeviceWatcherEventKind})
    GetBackgroundTrigger(Windows.Foundation.Collections.IIterable{Windows.Devices.Enumeration.DeviceWatcherEventKind})

    Gets a DeviceWatcherTrigger object monitoring for changes to the list of devices.

    public DeviceWatcherTrigger GetBackgroundTrigger(Windows.Foundation.Collections.IIterable{Windows.Devices.Enumeration.DeviceWatcherEventKind} requestedEventKinds)public DeviceWatcherTrigger GetBackgroundTrigger(Windows.Foundation.Collections.IIterable{Windows.Devices.Enumeration.DeviceWatcherEventKind} requestedEventKinds)Public Function GetBackgroundTrigger(requestedEventKinds As Windows.Foundation.Collections.IIterable{Windows.Devices.Enumeration.DeviceWatcherEventKind}) As DeviceWatcherTrigger

    Parameters

    • requestedEventKinds

      A list of the specific updates you want to monitor.

    Returns

  • Start()
    Start()
    Start()
    Start()

    Starts a search for devices, and subscribes to device enumeration events.

    public void Start()public void Start()Public Function Start() As void

    Remarks

    An app calls Start() to begin the search for devices. During this intial 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 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(). The following diagram shows how the DeviceWatcher transitions between the states represented by DeviceWatcherStatus enumeration.

    state diagram of DeviceWatcher states

  • Stop()
    Stop()
    Stop()
    Stop()

    Stop raising the events that add, update and remove enumeration results.

    public void Stop()public void Stop()Public Function Stop() As void

    Remarks

    To stop a search for devices, an app calls Stop() and enters the Stopping state. The Stopped event is raised when the Stop() operation completes and the DeviceWatcher enters the Stopped state.

    The following diagram shows how the DeviceWatcher transitions between the states represented by DeviceWatcherStatus enumeration.

    state diagram of DeviceWatcher states

    This call 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.

    Callers 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.

Events

  • Added
    Added
    Added
    Added

    Event that is raised when a device is added to the collection enumerated by the DeviceWatcher.

    public event TypedEventHandler Addedpublic event TypedEventHandler AddedPublic Event Added

    Remarks

    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.

  • EnumerationCompleted
    EnumerationCompleted
    EnumerationCompleted
    EnumerationCompleted

    Event that is raised when the enumeration of devices completes.

    public event TypedEventHandler EnumerationCompletedpublic event TypedEventHandler EnumerationCompletedPublic Event EnumerationCompleted
  • Removed
    Removed
    Removed
    Removed

    Event that is raised when a device is removed from the collection of enumerated devices.

    public event TypedEventHandler Removedpublic event TypedEventHandler RemovedPublic Event Removed

    Remarks

    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.

  • Stopped
    Stopped
    Stopped
    Stopped

    Event that is raised when the enumeration operation has been stopped.

    public event TypedEventHandler Stoppedpublic event TypedEventHandler StoppedPublic Event Stopped
  • Updated
    Updated
    Updated
    Updated

    Event that is raised when a device is updated in the collection of enumerated devices.

    public event TypedEventHandler Updatedpublic event TypedEventHandler UpdatedPublic Event Updated

    Remarks

    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.

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.DualApiPartitionAttribute
Windows.Foundation.Metadata.ContractVersionAttribute

Details

Assembly

Windows.Devices.Enumeration.dll