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條經(jīng)驗 獲得超16個贊
這樣設(shè)置試試看??!
[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);
- 2 回答
- 0 關(guān)注
- 570 瀏覽
添加回答
舉報
0/150
提交
取消