all groups > dotnet sdk > february 2005 >
You're in the

dotnet sdk

group:

WMI - Cannot access form loaded from event


WMI - Cannot access form loaded from event Lance
2/1/2005 9:13:03 AM
dotnet sdk: I need to have a form load whenever a new serial port is added to the system.

I've created an EventWatcher to perform this and the event is triggered as
expected. However, the form that is loaded from within the event is frozen.
The mouse is in the hourglass, the form cannot be clicked, moved, closed,
activated, and does not properly display itself. I've checked to see if it
is a problem with the form, however even if the form has no controls/code
other than the windows required code, it still behaves in this fashion.

Can anyone give me some insight as to why this is happening, or a possible
alternative method.

Code snippet:

Private Class PortMonitor
Private w As ManagementEventWatcher = Nothing

Public Sub Start()
Dim q As WqlEventQuery = New WqlEventQuery
q.EventClassName = "__InstanceCreationEvent"
q.WithinInterval = New TimeSpan(0, 0, 3)
q.Condition = "TargetInstance ISA 'Win32_SerialPort'"
w = New ManagementEventWatcher(q)
AddHandler w.EventArrived, New
EventArrivedEventHandler(AddressOf Me.OnPortArrived)
w.Start()
End Sub

Public Sub [Stop]()
RemoveHandler w.EventArrived, New
EventArrivedEventHandler(AddressOf Me.OnPortArrived)
w.Stop()
w.Dispose()
End Sub

Protected Sub OnPortArrived(ByVal sender As Object, ByVal e As
EventArrivedEventArgs)
Dim f As New Form1
f.Show() ' Form is locked up!
End Sub
End Class

Thanks
Re: WMI - Cannot access form loaded from event Willy Denoyette [MVP]
2/5/2005 6:11:14 PM
The OnPortArrived eventhandler is running on a threadpool thread, NOT on the
UI thread. Threadpool threads have no message pump and shold not be used to
show windows UI elements!
What you should do is delegate the OnPortArrived event handling on the UI
thread, check the "MethodInvoker" method in MSDN.

Willy.


[quoted text, click to view]

Re: WMI - Cannot access form loaded from event Lance
2/7/2005 5:21:02 AM
Thanks Willy, I'll check it out.

[quoted text, click to view]
Re: WMI - Cannot access form loaded from event Bill Glass
2/9/2005 2:50:50 PM
Create an instance of this class in its own thread, and that will
make it work just fine.
--
Re: WMI - Cannot access form loaded from event Willy Denoyette [MVP]
2/11/2005 12:35:30 AM
No it won't, you can't touch the UI from another thread than the UI thread
itself.

Willy.


[quoted text, click to view]

AddThis Social Bookmark Button