views:

1229

answers:

2

I am using : http://www.codeproject.com/KB/IP/Facebook_API.aspx

I am trying to call the xaml which is created using WPF. But it gives me a

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

error

I don't know what to do. I am trying to do this:

FacebookApplication.FacebookFriendsList ffl = new FacebookFriendsList();

but it is giving me that error

Edit: added background worker

static BackgroundWorker bw = new BackgroundWorker();



        static void Main(string[] args)
        {


            bw.DoWork += bw_DoWork;

            bw.RunWorkerAsync("Message to worker");

            Console.ReadLine();

        }



        static void bw_DoWork(object sender, DoWorkEventArgs e)
        {

            // This is called on the worker thread

            FacebookApplication.FacebookFriendsList ffl = new FacebookFriendsList();
            Console.WriteLine(e.Argument);        // writes "Message to worker"

            // Perform time-consuming task...

        }
A: 

I suspect that you you are getting a callback to a UI component from background thread. I recommend that you make that call using a BackgroundWorker as this is UI thread aware.

Edit: Later: Your main program is marked as [STAThread] isn't it?

Preet Sangha
I tried adding it, as above, but it still gives me the error :/
C.
I'm not familliar with the code. Can you debug through and find out exactly the line of code causing this?
Preet Sangha
+1  A: 

If you make the call from the main thread, you must add the STAThread attribute to the Main method, as stated in the previous answer.

If you use a separate thread, it needs to be in a STA (single-threaded apartment), which is not the case for background worker threads. You have to create the thread yourself, like this:

Thread t = new Thread(ThreadProc);
t.SetApartmentState(ApartmentState.STA);

t.Start();

with ThreadProc being a delegate of type ThreadStart.

Timores