0

I'm thinking of using this code to detect if the thread is the main (UI) thread.

public static bool IsMainThread()
{
    return Thread.CurrentThread.GetApartmentState() == ApartmentState.STA;
}

It certainly appears to work fine with worker threads running tasks returning false, just wondering if it is generally true.

If it's relevant, this is a WPF application.

This is similar to my other question: How do I assert that the code is running in the main thread? But no one suggested this solution.

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203

3 Answers3

1

Yes, you can set STA on other threads when you create them. Thread pool threads are in the MTA.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
  • But unless I explicitly do this, then it won't be the case that multiple threads are STA? – weston Dec 03 '12 at 16:14
  • If you control all the thread creation in your app, then you're correctly, only the main thread will be STA - threads in .NET default to MTA. If you're hosting any COM components, or you're using a library that does, you may end up with STA thread that you don't know about. – Chris Tavares Dec 03 '12 at 17:40
  • Isn't there something on the dispatcher that lets you detect this more reliably? – Chris Tavares Dec 03 '12 at 17:41
  • Yes comparing the current thread dispatcher to a cached reference to the main thread dispatcher seems to be the way forward. – weston Dec 12 '12 at 19:56
1

Yes you can have multiple threads have STA apartment. But you MUST NOT swap framework elements between threads, that will still cause errors. Meaning you can't make a textbox control and populate it in a worker thread that is also STA and then pass it to you main thread (which is also STA) and add it to a form. That will still cause errors. So the question is, why do you want to set multiple threads to STA?

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
  • I don't want to. I just want a way of detecting the main thread. If it is highly unlikely/uncommon for multiple threads to have STA set, then this is probably good enough. – weston Dec 03 '12 at 16:13
1

According to the documentation for STAThreadAttribute, you can not have more than one thread that is STA (you can't even truely have one unless you are using COM interop).

"Apply this attribute to the entry point method (the Main() method in C# and Visual Basic). It has no effect on other methods."

Since .Net 2.0 the Single Threaded Apartment is only intended for use when performing COM interop.

"COM threading models only pertain to applications that use COM interop. Using this attribute in an application that does not use COM interop has no effect."

"In the .NET Framework version 2.0, new threads are initialized as ApartmentState.MTA if their apartment state has not been set before they are started. The main application thread is initialized to ApartmentState.MTA by default."

Reference: http://msdn.microsoft.com/en-us/library/system.stathreadattribute(v=vs.100).aspx

Kevin
  • 704
  • 3
  • 4