• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2760
  • Last Modified:

"The calling thread must be STA, because many UI components require this"

Hi Experts,

   First off, I am brand new to WPF.    I have a few threads that run in the background of my WPF desktop app.  In the mean time I have a global class that subscribes to the error event of all of these different BackgroundWorker threads.  When one of these error events fire, the event handler in the global class attempt to pop-up an error form.  When that happens I get a runtime error saying: "The calling thread must be STA, because many UI components require this"  I know that what is going on is that a non-UI thread is trying to create a UI object.  My problem is that I have no idea how to fix it.  In WinForms I would have done something like:

 
if (this.InvokeRequired)
            {
                NoParameterDelegate delegateObject = new NoParameterDelegate(this.RefreshGradient);
                this.Invoke(delegateObject, new object[] { });
            }
            else
            {
               UpDateMyUI();
            }

Open in new window


Thanks!
0
axnst2
Asked:
axnst2
  • 3
  • 2
1 Solution
 
axnst2Author Commented:
BTW, I am using .NET 4.0, VS2010
0
 
axnst2Author Commented:
No responses...

What I am trying to accomplish is a very simple thing to do in WinForms.  So it seems to me that WPF is great for very simple applications that you want to make look prety, but as soon as you try to do something besides showing a list of pictures or adding 2+2, it is absolutley useless.  Now if I could only get my boss to understand this...  It really is sad that Microsoft has decided that WPF is going to be the replacement for WinForms.  I see the technology as a step backwards not forward....  To me it seems that the technology was created to handle mindless applications that look very pretty.  Unfortunately, I need it to solve complex engineering problems and not sell makeup... PLEASE PROVE ME WRONG!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's pretty similar...just use the Dispatcher() instead of the Form (this):
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26370867.html#33333150
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*From the linked PAQ*

    "That doesn't solve my issue but it does provide me with a work around.  So how can I get refrence to the main window from else where?  Is there like a global or static reference to it?"

Referencing a window in WPF is no different than in WinForms.  You need to pass a reference to the form/class following standard OOP procedures...
0
 
axnst2Author Commented:
Thanks!  I am starting to realize that with WPF I have to have a completely different mind set all together.  It's like I have to unlearn everything I know to work and learn a whole new way of looking at things...this is going to be one steeeeeeeeepppp a.. learning curve......

Thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now