Link to home
Start Free TrialLog in
Avatar of axnst2
axnst2Flag for United States of America

asked on

"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!
Avatar of axnst2
axnst2
Flag of United States of America image

ASKER

BTW, I am using .NET 4.0, VS2010
Avatar of axnst2

ASKER

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!
Avatar of Mike Tomlinson
It's pretty similar...just use the Dispatcher() instead of the Form (this):
https://www.experts-exchange.com/questions/26370867/C-Updating-WPF-with-from-Thread.html#33333150
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of axnst2

ASKER

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!