14

I have a WPF application in which a thread checks some value. In certain cases, I show a pop-up Window in order to display a message. When I create this pop-up window in the thread, an exception is thrown by the pop-up window's constructor:

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

How do I resolve this error?

This is my code for creating the pop-up window:

// using System.Threading;
// using System.Windows.Threading;
Thread Messagethread = new Thread(new ThreadStart(delegate()
{
    DispatcherOperation DispacherOP = 
        frmMassenger.Dispatcher.BeginInvoke(
            DispatcherPriority.Normal,
            new Action(delegate()
            {
                frmMassenger.Show();
            }));
}));
Messagethread.Start();
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Rev
  • 2,269
  • 8
  • 45
  • 75
  • 1
    I found Problem Root. I have a timer which in the each timer.tick , check for new message`s and then show each new message. Timer thread make my work so hard. – Rev Apr 18 '10 at 10:00
  • 1
    Off-topic: In WPF, windows are called just that: "windows"; not "forms". That latter term is used in another UI framework, Windows Forms. – stakx - no longer contributing Aug 20 '13 at 07:48
  • Possible duplicate of [The calling thread must be STA, because many UI components require this](http://stackoverflow.com/questions/2329978/the-calling-thread-must-be-sta-because-many-ui-components-require-this) – naXa stands with Ukraine Sep 17 '16 at 20:27

2 Answers2

16

For the thread that you're trying to start the GUI element in, you need to set the apartment state of the thread to STA BEFORE you start it.

Example:

myThread.SetApartmentState(ApartmentState.STA);
myThread.Start();
archer
  • 262
  • 2
  • 15
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • @Rev: Let me know, if it doesn't get past your error let me know the error message and I'll provide the extra info you need. I think that's all you'll need though. – Brian R. Bondy Apr 17 '10 at 16:58
  • thx for help but that doesn't work. this problem related to Wpf and Threading.I think We Should use Dispatcher for solving that. Regards Rev – Rev Apr 18 '10 at 04:44
  • 2
    @Rev: Probably you have 2 issues. The threading STA and the dispatcher. If you want help with the dispatcher let me know and I can show you how. – Brian R. Bondy Apr 18 '10 at 04:47
  • I think dispatcher can help me. ;) – Rev Apr 18 '10 at 09:56
10

Absolutely Dispatcher is only way to do something (in specific Thread) when we work with multi-threading in WPF!

But for work with Dispatcher we must know 2 things:

  1. Too many way to use Dispatcher like Dispatcher_Operation , [window.dispatcher] or etc.
  2. We must call dispatcher in the main thread of app (that thread is must be STA thread)

So for example: if we want show other window[wpf] in another thread, we can use this code:

Frmexample frmexample = new Frmexample();
            frmexample .Dispatcher.BeginInvoke //Updated the variable name
                (System.Windows.Threading.DispatcherPriority.Normal,
                (Action)(() =>
                {
                    frmexample.Show();
                    //---or do any thing you want with that form
                }
                ));

Tip: Remember - we can't access any fields or properties from out dispatcher, so use that wisely

Abdul-Razak Adam
  • 1,070
  • 11
  • 19
Rev
  • 2,269
  • 8
  • 45
  • 75