Question WMI WndProc Not Mixing Well

Ken W

Member
Joined
Jul 23, 2010
Messages
9
Programming Experience
3-5
I'm using the WndProc To get messages about USB flashdrives connecting and removing from the users computer. It works grand. I also use WMI queries to get the info about the flashdrives. It works as it shouold. The problem is when both of these are in the same solution. I ended up creating an extra .EXE that uses the WMI queries to get the drive info and output the info to a CSV file which my Main will parse. I tries creating a seperate class of each and i also tried to make a DLL that I import to no avail.

Does anyone have a way to avoid this what i think is a waste of code and time? Any help would be greatly appreciated.

Ken Indiana USA
 
I did a test and also got an exception calling WMI from WndProc, not sure why, but if I made the call from a different thread all was ok. (I just did a delegate.BeginInvoke) This is something you should do anyway since WndProc is pretty busy and WMI is pretty slow. Put a 100ms thread.sleep in WndProc for laughs and watch your app put to a crawl! What info is it you request with WMI anyway?
 
Thanks for the reply. I just get the flashdrive insert/remove events and when i receive the event I display the available flashdrives to the user. I'm using the following. The reason I am using it is because the My.Computer.FileSystem.GetDriveInfo() is way to slow.

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DoDrives() [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE]
[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ret [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE][/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] searcher [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ManagementObjectSearcher([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"root\CIMV2"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"SELECT * FROM Win32_LogicalDisk WHERE (DriveType = 2) "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & _[/SIZE]
[SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"And FreeSpace > 1000000"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] queryObj [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ManagementObject[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] queryObj [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] searcher.Get()[/SIZE]
[SIZE=2]addLogicalDrive(queryObj([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"VolumeName"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]), queryObj([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"DeviceID"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]), queryObj([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"FileSystem"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]), queryObj([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"FreeSpace"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).ToString, _[/SIZE]
[SIZE=2]queryObj([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Name"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]), queryObj([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Size"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).ToString, queryObj([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"VolumeSerialNumber"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).ToString)[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][/COLOR][/SIZE]
 
 
[SIZE=2]ret = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] err [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ManagementException[/SIZE]
 
[SIZE=2]MessageBox.Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"An error occurred while querying for WMI data: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & err.Message)[/SIZE]
 
[SIZE=2]ret = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE][/COLOR][/SIZE]
 
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ret[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][/COLOR][/SIZE]
 
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] addLogicalDrive([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Description [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DeviceID [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FileSystem [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FreeSpace [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Name [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Size [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SerialNumber [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2]lstDescription.Add(Description)[/SIZE]
[SIZE=2]lstDriveId.Add(DeviceID)[/SIZE]
[SIZE=2]lstFileSystem.Add(FileSystem)[/SIZE]
[SIZE=2]lstFreeSpace.Add(FreeSpace)[/SIZE]
[SIZE=2]lstName.Add(Name)[/SIZE]
[SIZE=2]lstDriveSize.Add(Size)[/SIZE]
[SIZE=2]lstSerialNumber.Add(SerialNumber)[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/COLOR][/SIZE]
 
Ok solution time. No thanks to M$ though. I had this View attachment 2496 when the WMI Sub was in the same solution whether it be a class or an imported dll. I tried threading it and using the backgroundworker component. (see Attachment 2496) for result. So I created another .EXE that just queries the system whenever I want to call it (WMI). It outputs the FlashDrive Info I want to a CSV file and I parse it in the original App. No more (see Attachment 2496). The speed, which I can't believe, is amazing. It's still way faster than using My.Computer.FileSystem.GetDriveInfo() although it doesn't throw the error.

When a flashdrive is inserted you get the window that asks what you want to do and even before I clear that window my info is in the status textbox and the drives are listed in the combobox. If the flashdrive is removed it's even quicker.

Not what I thought but It works. I'm still looking into this and would appreciate any advice.


Have a nice day

Ken, Indiana
 
As I said, processing on secondary thread worked fine for me. I have no other advice.
 
Back
Top