Reputation: 14375
I have a C# Windows Forms app that runs on Windows 8.1 or newer and does speech recognition. I want to be notified when a new USB audio input device is connected to the system. I am hoping there is a notification service in the Windows API that will tell me when and audio device is connected to the system or disconnected from the system.
Is there such a notification available, or do I have constantly rescan the available audio input devices and create my own notifications when I detect a change? I'd obviously don't want to reinvent the wheel.
Upvotes: 2
Views: 1457
Reputation: 634
The following will listen for USB plug-in/turn-on un-plug/turn-off at a scan rate of 2seconds intervals
//turn on USB device event
WqlEventQuery q_creation = new WqlEventQuery();
q_creation.EventClassName = "__InstanceCreationEvent";
q_creation.WithinInterval = new TimeSpan(0, 0, 2);
q_creation.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
mwe_creation = new ManagementEventWatcher(q_creation);
mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
mwe_creation.Start();
//turn off USB device event
WqlEventQuery q_deletion = new WqlEventQuery();
q_deletion.EventClassName = "__InstanceDeletionEvent";
q_deletion.WithinInterval = new TimeSpan(0, 0, 2);
q_deletion.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice' ";
mwe_deletion = new ManagementEventWatcher(q_deletion);
mwe_deletion.EventArrived += new EventArrivedEventHandler(USBEventArrived_Deletion);
mwe_deletion.Start();
private void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
{
}
private void USBEventArrived_Deletion(object sender, EventArrivedEventArgs e)
{
}
Upvotes: 3