异常System.Threading.ThreadStateEx C#.. [英] Exception System.Threading.ThreadStateEx C#..

查看:196
本文介绍了异常System.Threading.ThreadStateEx C#..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的安装程序中,我正在使用Windows应用程序.在此Windows应用程序中,我具有Folderdialog控件.我以3种不同形式添加了相同的控件.我在这里使用以下代码

Hi,

In my installer I am using windows application. In this windows application I have Folderdialog control. The same control I added in 3 different forms. Here I am using the following code

Thread thFileDialog = new Thread(new ParameterizedThreadStart(OpenFileDialog));
                thFileDialog.SetApartmentState(ApartmentState.STA);
                thFileDialog.Start();


我正在使用的Openfiledialog方法打开文件对话框.

我已经在主要功能中使用了[STAThread] attrubute.

但是我遇到一个错误,例如" System.Threading.ThreadStateException:必须先将当前线程设置为单线程单元[STA]模式,然后才能进行OLE调用.".

这是我追求的四种形式.这不会影响我的功能.但是我想解决它.解决方法.

谢谢.


That Openfiledialog method I am using to open a file dialog.

I already used [STAThread] attrubute in main function.

But I am getting one error like ''System.Threading.ThreadStateException: Current thread must be set to single thread apartment [STA] mode before OLE calls can be made.''.

This I am getting after 4 forms. This is not affecting my functionality. But I want to resolve it. How to solve it.

Thanks.

推荐答案

问题很奇怪,因为您正确地将它设置为STA.

我只能怀疑一点:请检查运行应用程序入口点的线程的单元状态(通常为Main).如果这是WPF或Forms应用程序,则此线程也可以是STA(对于WPF,必须是).

确保您的入口点看起来像这样:
The problem is strange as you do make it STA, correctly.

There is only one point I can suspect: please check up the apartment state of the thread running your application''s entry point (normally, Main). If this is WPF or Forms application, this thread also can be STA (and for WPF, it must be).

Make sure your entry point looks like this:
[System.STAThread]
static void Main() { /* ... */ }



如果以这种方式完成此操作,则无需调用SetApartmentState,除非您使用MTA从另一个线程启动了一个线程.对于WPF,除非您明确执行此代码,否则它将自动生成.

如果这样做没有帮助,请检查您在其他位置设置了其他公寓状态.

您还可以在每个线程的主体中检查单元线程,以进行调试.

—SA



If this is done this way, there is no need in calling SetApartmentState, unless you start a thread from the other thread with MTA. For WPF, this code is auto-generated unless you do it explicitly.

If this does not help, check up where else you set up different apartment state.

You can also check up apartment thread in the body of each thread, for debugging purpose.

—SA


这篇关于异常System.Threading.ThreadStateEx C#..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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