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

facebookmultithreadingwpfxaml

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...
}

Best Answer

Try to invoke your code from the dispatcher:

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