Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / February 2005

Tip: Looking for answers? Try searching our database.

WMI - Cannot access form loaded from event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lance - 01 Feb 2005 17:13 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.