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
Willy Denoyette [MVP] - 05 Feb 2005 17:11 GMT
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.
>I need to have a form load whenever a new serial port is added to the
>system.
[quoted text clipped - 42 lines]
>
> Thanks
Lance - 07 Feb 2005 13:21 GMT
Thanks Willy, I'll check it out.
> 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
[quoted text clipped - 50 lines]
> >
> > Thanks
Bill Glass - 09 Feb 2005 22:50 GMT
Create an instance of this class in its own thread, and that will
make it work just fine.
Willy Denoyette [MVP] - 10 Feb 2005 23:35 GMT
No it won't, you can't touch the UI from another thread than the UI thread
itself.
Willy.
> Create an instance of this class in its own thread, and that will
> make it work just fine.
> --
> POST BY: http://www.dotNET.us - Need .NET? Just ask, Please dotNET.us