12-01-2008 01:49 AM
Hello. I want to recieve an event notification when the USB device is plug in or unplugged from the system. There is a special System.Management assembly which allows to catch system messages (also notifications from USB controller). I've tried to rebuilt some easy C# examples on LV using .NET event callback chain, it seems I'm missing something (See attached VI's).
On code example:
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class WMIReceiveEvent
{
public static void Main()
{
try
{
WqlEventQuery query = new WqlEventQuery(
"SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance ISA 'Win32_USBControllerDevice'");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
Console.WriteLine("Waiting for an event...");
ManagementBaseObject eventObj = watcher.WaitForNextEvent();
Console.WriteLine("{0} event occurred.", eventObj["__CLASS"]);
// Cancel the event subscription
watcher.Stop();
return;
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
}
}
}
}
Also here is some useful links:
http://dotnetslackers.com/community/blogs/basharkokash/archive/2008/03/15/USB-Detection-source-code.aspx
http://www.developerfusion.com/forum/thread/53237/
Any help will be appreciated.
Best Regards, Konstantin.
12-01-2008 05:48 AM - edited 12-01-2008 05:49 AM
A search of the NI forums for "USB Event notification" returned an entry that may help you:
Windows device event notification
12-01-2008 06:10 AM
I couldn't look at your code initially, I usually work on a LabVIEW 7.0 machine.
You may want to look at the .NET PowerModeChanged Notifier example I coded some time ago. It's been used by others, and should help you understand how to capture .NET events and convert them to LabVIEW events...
12-02-2008 02:23 AM
Hi there
.NET events and LabVIEW aren't working together in most cases. I redraw your C# code, seems to work (see attachment). You have to wait a few seconds for the event to arrive.
12-02-2008 07:04 AM
Hi,chrisger, thanks for help. Could you resave the VI into 8.2 version? About .NET events: may be there are some problems with delegating from LV side etc.
12-02-2008 07:08 AM
there you are...