We help IT Professionals succeed at work.

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

axnst2
axnst2 asked
on
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!
Comment
Watch Question

axnst2IT Manager

Author

Commented:
BTW, I am using .NET 4.0, VS2010
axnst2IT Manager

Author

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!
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
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
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
*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...
axnst2IT Manager

Author

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!