We help IT Professionals succeed at work.
Get Started

C# WPF STA thread

deleyd
deleyd asked
on
91 Views
Last Modified: 2020-01-21
I encountered some C# WPF code (written by other people) and I think the problem they were trying to solve was displaying a message window from a background thread, and getting the error "The calling thread must be STA, because many UI components require this."

So it looks like they decided to fix this by creating their background thread and setting the ApartmentState to STA before running it.

I'm skeptical if this is a valid solution.

I'd like to know if I can prove this can lead to problems, or is there some official documentation somewhere that says "Don't do this."

(Because I eventually need to make a decent argument that will be convincing to others.)

(I'm used to instead using Dispatch to run code on the UI thread.)

(I do admit there is a need to somehow deal with "the background thread encountered a problem and we need to tell someone about it". I suspect setting the background thread to STA is not the proper solution.)
Comment
Watch Question
Architect - Coder - Mentor
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE