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

C.. picture C.. · Feb 24, 2010 · Viewed 216.6k times · Source

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 an error:

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

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.

I added a 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...
}

Answer

Amjad Abdelrahman picture Amjad Abdelrahman · Feb 3, 2014

Try to invoke your code from the dispatcher:

Application.Current.Dispatcher.Invoke((Action)delegate{
      // your code
});