More WMI events
I was playing around with using the wmi _InstanceModificationEvent. I created a sample which will keep an acurate list of the screen size. Like all wmi programs you need to add a reference to system.management for this to work. My sample requires a listbox on a form.
Imports System.Management
Public Class Form1
Dim WithEvents w As ManagementEventWatcher
Dim q As WqlEventQuery
Delegate Sub LoadList()
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
w.Stop()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
q = New WqlEventQuery
q.QueryString = "SELECT * FROM" & _
" __InstanceModificationEvent WITHIN 1 " & _
"WHERE TargetInstance isa ""Win32_DisplayConfiguration"""
w = New ManagementEventWatcher(q)
w.Start()
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
GetDeviceSettings()
End Sub
Private Sub GetDeviceSettings()
ListBox1.Items.Clear()
Dim moReturn As Management.ManagementObjectCollection
Dim moSearch As Management.ManagementObjectSearcher
Dim mo As Management.ManagementObject
moSearch = New Management.ManagementObjectSearcher("Select * from Win32_DisplayConfiguration")
moReturn = moSearch.Get
For Each mo In moReturn
ListBox1.Items.Add(mo("Caption").ToString)
ListBox1.Items.Add(String.Format("Height {0}", mo("PelsHeight")))
ListBox1.Items.Add(String.Format("Width {0}", mo("PelsWidth")))
Next
End Sub
Private Sub w_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles w.EventArrived
For Each p As Process In Process.GetProcesses
Trace.WriteLine(p.MainWindowTitle)
Next
ListBox1.Invoke(New LoadList(AddressOf GetDeviceSettings))
End Sub
End Class