调用线程必须是sta,因为许多ui组件都需要 [英] the calling thread must be sta because many ui components require

查看:89
本文介绍了调用线程必须是sta,因为许多ui组件都需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在保存视图的同时显示动画进度条
上面的代码是我正在尝试的代码,但它给了我一个错误,说:

"调用线程必须是STA,因为许多UI组件都需要STA. 错误出现在我正在加载的Page的构造函数上.

我现在不知道该怎么办.有什么方法可以使它更简单,因为这是我第一次使用BackgroundWorker.

I want to show the animated progressbar while saving view
the above code is the one I'm experimenting on but it gives me an error saying:

" The calling thread must be STA, because many UI components require this."
The error appears on the constructor of the Page i'm loading.

I have no idea now what to do. Is there any way I can make this simpler as this is my frist time to use BackgroundWorker..

private void OnScreenShotAllCommandExecuted(object parameter) { UserControl view = new UserControl();

view.Show(); SaveAllScreen saveScerrn = new SaveAllScreen(); worker.RunWorkerAsync(saveScerrn); } 私有void worker_DoWork(对象发送者,DoWorkEventArgs e) { SaveAllScreen.OnScreenShotAllCommand(); 如果(worker.CancellationPending) { e.Cancel = true; 返回; } } 私有void worker_RunWorkerCompleted(对象发送者,RunWorkerCompletedEventArgs e) { 如果(已取消) { MessageBox.Show(搜索已取消."); } 否则if(e.Error!= null) { //DoWork事件处理程序引发了错误. MessageBox.Show(e.Error.Message,发生错误"); } }

view.Show(); SaveAllScreen saveScerrn = new SaveAllScreen(); worker.RunWorkerAsync(saveScerrn); } private void worker_DoWork(object sender, DoWorkEventArgs e) { SaveAllScreen.OnScreenShotAllCommand(); if (worker.CancellationPending) { e.Cancel = true; return; } } private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { MessageBox.Show("Search cancelled."); } else if (e.Error != null) { // An error was thrown by the DoWork event handler. MessageBox.Show(e.Error.Message, "An Error Occurred"); } }


推荐答案

特别是,WPF应用程序UI线程本身需要STA.这很容易解决.对于主线程,这由应用于应用程序入口点的属性控制:

In particular, WPF application UI thread itself requires STA. This is easy to fix. For a main thread, this is controlled by the attribute applied to the application entry point:

[STAThread]
static void Main() {
    //...
}


实际上,这是为进程的启动线程设置线程单元状态的唯一有效方法,原因如下所述.

您可能会认为WPF应用程序中不存在这种情况,但事实并非如此.该代码只是由应用程序模板为您生成的(您可以在构建项目时在项目下找到它,只需进行一些文本搜索即可),但是在某些情况下,您可以 必须手动编写这部分代码,以实现某些自定义行为.

现在,剩下的问题是为运行时创建的线程设置单元状态,仅当该线程使用的某些代码需要它时才需要执行此操作.这也不是问题.要了解的一件事是:线程无法更改 它的单元状态由在此线程中运行的代码组成.您只能在其他线程中(通常是在创建和启动该线程的代码中)并且只能在启动之前执行此操作.此处: http://msdn.microsoft.com/en-us/library/system.threading.thread.setapartmentstate.aspx


This is actually the only valid way to set a thread Apartment State for the start-up thread of the process, by the reason I explain below.

You may think that such thing does not exist in a WPF application, but that's not true. This code is simply generated for you by the application template (you can find it under your project when it is build, just do some text search), but in some cases you have to write this part of code manually, to achieve some customized behavior.

Now, the remaining problem is to set up the Apartment State for the thread you create during run time, which you need to do only if it is required by some code used by the thread. This is also not a problem. One thing to understand is: a thread cannot change its Apartment State by the code running in this thread. You can only do it in some other thread, usually the code where the thread is created and started, and only before starting it. Here: http://msdn.microsoft.com/en-us/library/system.threading.thread.setapartmentstate.aspx

检查以下内容: http://www.codeproject.com/Questions/597334/WPFplusErrorplusopeningpluswindowplusfromplusmain

http://stackoverflow.com/questions/10733000/the-calling-thread-must-be-sta-because-many-ui-components-require-this-wpf

这篇关于调用线程必须是sta,因为许多ui组件都需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆