wpf 调用线程必须是STA,因为很多UI组件都需要这个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2329978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The calling thread must be STA, because many UI components require this
提问by C..
I am using http://www.codeproject.com/KB/IP/Facebook_API.aspx
我正在使用http://www.codeproject.com/KB/IP/Facebook_API.aspx
I am trying to call the XAMLwhich is created using WPF. But it gives me an error:
我正在尝试调用使用WPF创建的XAML。但它给了我一个错误:
The calling thread must be STA, because many UI components require this.
调用线程必须是 STA,因为许多 UI 组件都需要它。
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...
}
回答by Amjad Abdelrahman
Try to invoke your code from the dispatcher:
尝试从调度程序调用您的代码:
Application.Current.Dispatcher.Invoke((Action)delegate{
// your code
});
回答by Timores
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.
如果您从主线程进行调用,则必须将 STAThread 属性添加到 Main 方法,如上一个答案中所述。
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:
如果使用单独的线程,则需要在 STA(单线程单元)中,而后台工作线程则不是这种情况。您必须自己创建线程,如下所示:
Thread t = new Thread(ThreadProc);
t.SetApartmentState(ApartmentState.STA);
t.Start();
with ThreadProc being a delegate of type ThreadStart.
ThreadProc 是 ThreadStart 类型的委托。
回答by atik sarker
You can also try this
你也可以试试这个
// 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();
回答by Preet Sangha
I suspect that you are getting a callback to a UI component from a background thread. I recommend that you make that call using a BackgroundWorker as this is UI thread aware.
我怀疑您正在从后台线程获取对 UI 组件的回调。我建议您使用 BackgroundWorker 进行该调用,因为这是 UI 线程感知的。
For the BackgroundWorker, the main program should be marked as [STAThread].
对于BackgroundWorker,主程序应标记为[STAThread]。
回答by LiRoN
Just mark your program with the [STAThread]
attribute and the error goes away! it's magic :)
只需用[STAThread]
属性标记您的程序,错误就会消失!这是魔法 :)
回答by Ryan Loggerythm
For me, this error occurred because of a null parameter being passed. Checking the variable values fixed my issue without having to change the code. I used BackgroundWorker.
对我来说,发生此错误是因为传递了空参数。检查变量值解决了我的问题,而无需更改代码。我使用了BackgroundWorker。
回答by Balvant Ramani
If you call a new window UI statement in an existing thread, it throws an error. Instead of that create a new thread inside the main thread and write the window UI statement in the new child thread.
如果在现有线程中调用新窗口 UI 语句,则会引发错误。而不是在主线程中创建一个新线程并在新子线程中编写窗口 UI 语句。