views:

513

answers:

3

Hi.

I make pop-up message for my application. this form is on wpf, and in the a thread i check a value and for some result , I show that pop-up to show message.

In the thread when I create pop-up class(type of WPF window class) an error throw by pop-up Contractor. "The calling thread must be STA, because many UI components require this"

What do I do?

//-------new edit :(

I use this for show my form in thread but still have error !!(cuz thread.start() in the timer thread)

System.Threading.Thread Messagethread = new System.Threading.Thread(
                new System.Threading.ThreadStart(delegate()
                    {
                        System.Windows.Threading.DispatcherOperation DispacherOP = frmMassenger.Dispatcher
                            .BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(
                                delegate()
                                {
                                       frmMassenger.Show();
                                }
                        ));
                    } ));
            Messagethread.Start();
+1  A: 

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

Example:

myThread.SetApartmentState(ApartmentState.STA);
myThread.Start();
Brian R. Bondy
i use this code before mythread.ApartmentState=ApartmentState.STA;i trying test ur solutionif that work u are my hero!! :)
Rev
@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
thx for helpbut that doesn't work.this problem related to Wpf and Threading.I think We Should use Dispatcher for solving that.Regards Rev
Rev
@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
I think dispatcher can help me.;)
Rev
my problem is still existbut In |WPF| we (programmers) Must use dispatcher for using Threading!:)
Rev
A: 

Or put the STAThread attribute on your program's entry point method (usually Main):

[STAThread]
public static void Main(string[] args)
{
    ...
}
stakx
i try this solution before. not working
Rev
A: 

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] ,...
2)We must call dispatcher in the main thread of app(that thread is STA thread)

so for example if u want show other window[wpf] in another thread u can use this code:

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

tip: remember u cant access any fields or properties from out dispatcher, so use that wisely

Rev