首页 新闻 会员 周边

vs2010 rc 调试winform怎么总是 MTA呢?

0
悬赏园豆:50 [已关闭问题]

main() 前面有 [STAThread]

 

            Thread.CurrentThread.ApartmentState = ApartmentState.STA;
            Debug.WriteLine(Thread.CurrentThread.ApartmentState);
            Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
            Debug.WriteLine(Thread.CurrentThread.GetApartmentState());

Thread.CurrentThread.ApartmentState = ApartmentState.STA;           Debug.WriteLine(Thread.CurrentThread.ApartmentState);

// 这里还是显示是 MTA

Thread.CurrentThread.SetApartmentState(ApartmentState.STA);          

// 这里会出异常:Failed to set the specified COM apartment state.

Debug.WriteLine(Thread.CurrentThread.GetApartmentState());

// 把上面那行去掉,这里还是显示 MTA

if (openFileDialog.ShowDialog() == DialogResult.OK)

// 异常:Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

这是为什么呢?

 

问题补充: 那补丁不是说解决一个crash问题的么 =,= 补充一下,我是在2010里调试.net3.5的项目(编译选项里是3.5)不是4.0的。 @langford 我不想起新线程呀。主线程不能使STA么? [STAThread] static void Main() { MessageBox.Show(Thread.CurrentThread.GetApartmentState().ToString()); } 如果直接运行(ctrl+f5)他就是STA,如果debug(f5)就显示MTA,这问题太恶心啦,有人遇到过么?
Fisher.W的主页 Fisher.W | 初学一级 | 园豆:0
提问于:2010-03-02 09:33
< >
分享
其他回答(2)
0

是不是补丁没有安装呢?

建议先安装下这个补丁试试

http://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=26662

风影极光 | 园豆:1573 (小虾三级) | 2010-03-02 09:38
0

可能是BUG引起的,在VS2008测试了一下,没有上述的问题,建议安装补丁

查尔斯 | 园豆:3832 (老鸟四级) | 2010-03-02 12:20
0

这样设置试试看!!

[STAThread]    static void Main() 
Thread  app  =  new  Thread(new  ParameterizedThreadStart(ThreadMethod));  app.ApartmentState  =  ApartmentState.STA;  
或Thread newThread = new Thread(new ThreadStart(ThreadMethod)); newThread.SetApartmentState(ApartmentState.MTA);

langford | 园豆:710 (小虾三级) | 2010-03-02 14:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册