Very high memory usage?

  • Thread starter Thread starter Brad Markisohn
  • Start date Start date
B

Brad Markisohn

I want to be notified when a PnP device is added or removed. To accomplish
this task, I added two occurrences of a
System.Management.ManagementEventWatcher. I've included a snippet of code
that is called when the user pushes a button to start listening. Using the
Windows Task manager I can monitor the CPU usage. When the watchers are
active, the CPU usage spikes to 100%. Turning of the watchers reduces the
CPU usage back to it's quiescent state. Is this expected behavior, or am I
doing something incorrectly? Is there a better way to monitor for PnP
devices being added or removed without incurring this CPU overhead?

TIA

Brad

Code example:
Public Sub StartListen()

Newwatcher = New System.Management.ManagementEventWatcher

Newwatcher.Query = New System.Management.EventQuery("SELECT * FROM
__InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA
'Win32_PnPEntity'")

AddHandler Newwatcher.EventArrived, AddressOf NewPlugPlay

Newwatcher.Start()

Removewatcher = New System.Management.ManagementEventWatcher

Removewatcher.Query = New System.Management.EventQuery("SELECT * FROM
__InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA
'Win32_PnPEntity'")

AddHandler Removewatcher.EventArrived, AddressOf RemovedPlugPlay

Removewatcher.Start()

End Sub
 
Hi Brad,

Based on my understanding, when you enable the ManagementEventWatcher the
CPU will spike to 100%, it will not reduce until you stop the
ManagementEventWatcher .

Based on my test, I can not reproduce the problem.
Here goes my test code.

Dim watcher As System.Management.ManagementEventWatcher
Public Sub NewPlugPlay(ByVal sender As Object, ByVal e As
EventArrivedEventArgs)
MsgBox("New Plug and Play = " +
(CType(e.NewEvent("TargetInstance"), ManagementBaseObject))("Caption"))
End Sub
Public Sub StartListen()
watcher = New System.Management.ManagementEventWatcher
'
' managementEventWatcher1
'
watcher.Query = New System.Management.EventQuery("SELECT * FROM
__InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA
""Win32_PnPEntity""")
AddHandler watcher.EventArrived, AddressOf NewPlugPlay
watcher.Start()
End Sub
Public Sub StopListen()
watcher.Stop()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
StartListen()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
StopListen()
End Sub

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Peter,

I guess that I'll have to live with CPU usage of 100% while monitoring PnP
device activity. I'm curious about the query:

SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA
""Win32_PnPEntity"""

Can you point me to a source that describes how this query, and others like
it, are created? This is not intuitive and I would've struggled without the
MSDN examples.

Thanks

Brad
 
Hi Brad,

Thanks for your quickly reply!

Since I can not reproduce the problem, I suggest you try to modify the
query statement as following.
SELECT * FROM __InstanceCreationEvent WITHIN 30 WHERE TargetInstance ISA
""Win32_PnPEntity"""

You may try to increase the query interval (e.g. change 1 to 30 or larger)
to see if the problem persists.

If you wants to understand WMI, I think the document in SDK is good and
necessary.
Windows Management Instrumentation
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
wmi_start_page.asp
Querying with WQL
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
querying_with_wql.asp


Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Peter,

Thanks for the pointer to the documentation. I'll start experimenting with
the query interval.

Brad
 
Back
Top