为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

收到一只叮咚 2018-10-24 05:06:30
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);// 这里还是显示是 MTAThread.CurrentThread.SetApartmentState(ApartmentState.STA);          // 这里会出异常:Failed to set the specified COM apartment state.Debug.WriteLine(Thread.CurrentThread.GetApartmentState());// 把上面那行去掉,这里还是显示 MTAif (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.这是为什么呢?
查看完整描述

2 回答

?
侃侃尔雅

TA贡献1801条经验 获得超15个赞

这样设置试试看!!

[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);


查看完整回答
反对 回复 2018-10-25
  • 2 回答
  • 0 关注
  • 553 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信