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] "Lance" <Lance@discussions.microsoft.com> wrote in message
news:CF88A7CA-5641-4479-A87B-65ED115ED7B7@microsoft.com...
>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
>