Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...
}
like image 284
C.. Avatar asked Oct 05 '22 22:10

C..


1 Answers

Try to invoke your code from the dispatcher:

Application.Current.Dispatcher.Invoke((Action)delegate{
      // your code
});
like image 300
Amjad Abdelrahman Avatar answered Oct 08 '22 11:10

Amjad Abdelrahman