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 / New Users / July 2006

Tip: Looking for answers? Try searching our database.

Gui threads (STA) and calling WMI

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arnie - 26 Jul 2006 19:21 GMT
Posting an issue for a colleague:

They are having an issue w/ making WMI calls from a gui thread. Gui threads
need to be on the STA and there is some speculation that WMI calls need to
be on the MTA, how to mix both?

...

The application is hanging on exit.

When they break into the debugger, the thread on which it is stuck is in a
'WaitOne' call.  The call stack is:

   mscorlib.dll!System.Threading.WaitHandle.WaitOne() + 0x57 bytes
   system.management.dll!System.Management.MTAHelper.WorkerThread() + 0x25
bytes

I searched around a bit on the web, and found this article:
http://discuss.develop.com/archives/wa.exe?A2=ind0306b&L=dotnet-clr&T=0&F=&S=&P=3961

It seems to imply that threads that make WMI calls must be MTA.

And, Paul thinks that GUI threads must be STA.  Many .NET controls are just
wrappers on COM controls, which would seem to imply they must be STA.

But, what about threads that are both GUI threads and make WMI calls?  Or
are both GUI threads and make MTA-COM calls?  They can't be both MTA and
STA...

Any thoughts?
Peter Ritchie [C# MVP] - 26 Jul 2006 20:12 GMT
Make the call on an anonymous asynchronous delegate.  For example:
public delegate void MyDelegate(IList list);
// ...
MyDelegate myDelegate = (MyDelegate)delegate(IList list)

    System.Diagnostics.Debug.Assert(System.Threading.Thread.CurrentThread.GetApartmentState() != System.Threading.ApartmentState.STA);
    ManagementObjectSearcher query;

    query = new ManagementObjectSearcher("SELECT * From Win32_Processor");
    foreach (ManagementObject obj in query.Get())
    {
        list.Add((string)obj["Name"]);
    }
};
IList stringList = new List<String>();
IAsyncResult asyncResult = myDelegate.BeginInvoke(stringList, null, null);
// block this thread until the asynchronous delegate is complete
myDelegate.EndInvoke(asyncResult);

Keep in mind, using WMI isn't normally a performant operation and may best
avoided on a GUI thread anyway (to keep it responsive).  In the above
example, it blocks the GUI thread until the operation is complete.  This is
just for example purposes.  I suggest using a callback and letting the GUI be
"free range" while the asynchronous delegate is doing it's work.

Signature

http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

> Posting an issue for a colleague:
>
[quoted text clipped - 26 lines]
>
> Any thoughts?
Michael Nemtsev - 26 Jul 2006 20:19 GMT
Hello Arnie,

Why not either to create new thread and set MTA property or to use ThreadPool's
thread which is MTA one?

A> Posting an issue for a colleague:
A>
A> They are having an issue w/ making WMI calls from a gui thread. Gui
A> threads need to be on the STA and there is some speculation that WMI
A> calls need to be on the MTA, how to mix both?
A>
A> ...
A>
A> The application is hanging on exit.
A>
A> When they break into the debugger, the thread on which it is stuck is
A> in a 'WaitOne' call.  The call stack is:
A>
A> mscorlib.dll!System.Threading.WaitHandle.WaitOne() + 0x57 bytes
A> system.management.dll!System.Management.MTAHelper.WorkerThread()
A> + 0x25
A> bytes
A> I searched around a bit on the web, and found this article:
A> http://discuss.develop.com/archives/wa.exe?A2=ind0306b&L=dotnet-clr&T
A> =0&F=&S=&P=3961
A>
A> It seems to imply that threads that make WMI calls must be MTA.
A>
A> And, Paul thinks that GUI threads must be STA.  Many .NET controls
A> are just wrappers on COM controls, which would seem to imply they
A> must be STA.
A>
A> But, what about threads that are both GUI threads and make WMI calls?
A> Or are both GUI threads and make MTA-COM calls?  They can't be both
A> MTA and STA...
A>
A> Any thoughts?
A>
---
WBR,
Michael  Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch

Rate this thread:







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.