C..*_*C.. 153 wpf xaml multithreading facebook
我正在使用http://www.codeproject.com/KB/IP/Facebook_API.aspx
调用线程必须是STA,因为许多UI组件都需要这个.
我不知道该怎么办.我想这样做:
FacebookApplication.FacebookFriendsList ffl = new FacebookFriendsList();
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误.
我添加了一个后台工作者:
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...
}
Run Code Online (Sandbox Code Playgroud)
Amj*_*man 192
尝试从调度程序调用您的代码:
Application.Current.Dispatcher.Invoke((Action)delegate{
// your code
});
Run Code Online (Sandbox Code Playgroud)
Tim*_*res 133
如果从主线程进行调用,则必须将STAThread属性添加到Main方法,如上一个答案中所述.
如果使用单独的线程,则需要在STA(单线程单元)中,而后台工作线程不是这种情况.你必须自己创建线程,如下所示:
Thread t = new Thread(ThreadProc);
t.SetApartmentState(ApartmentState.STA);
t.Start();
Run Code Online (Sandbox Code Playgroud)
ThreadProc是ThreadStart类型的委托.
Pre*_*gha 15
我怀疑你是从后台线程获得UI组件的回调.我建议您使用BackgroundWorker进行该调用,因为这是UI线程感知.
对于BackgroundWorker,主程序应标记为[STAThread].
ati*_*ker 14
你也可以试试这个
// create a thread
Thread newWindowThread = new Thread(new ThreadStart(() =>
{
// create and show the window
FaxImageLoad obj = new FaxImageLoad(destination);
obj.Show();
// start the Dispatcher processing
System.Windows.Threading.Dispatcher.Run();
}));
// set the apartment state
newWindowThread.SetApartmentState(ApartmentState.STA);
// make the thread a background thread
newWindowThread.IsBackground = true;
// start the thread
newWindowThread.Start();
Run Code Online (Sandbox Code Playgroud)
只需用[STAThread]
属性标记您的程序 Main 方法,错误就会消失!这是魔法 :)
例子:
class Program {
[STAThread]
static void Main(string[] args) {
// My code here
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
190678 次 |
最近记录: |