Problem with File Open Dialog
-
i am trying to write a small window form application. I use a file dialog to select a file in my computer. However, in the debug mode, when pressing the browse button, i got this error message : "An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll Additional information: 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." This problem happen only when i try to create a object of a class in the Form1.h file. If i delete the code of creating object, everything goes fine. I have checked the main function, it already had [STAThreadAttribute] marked on it. So, can you give me any suggestion, please? thanks
-
i am trying to write a small window form application. I use a file dialog to select a file in my computer. However, in the debug mode, when pressing the browse button, i got this error message : "An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll Additional information: 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." This problem happen only when i try to create a object of a class in the Form1.h file. If i delete the code of creating object, everything goes fine. I have checked the main function, it already had [STAThreadAttribute] marked on it. So, can you give me any suggestion, please? thanks
You must set the Apartment state of the thread, to be STA ( Single Threaded Apartment ) to work with OLE. // Run application Thread Thread _threadAppProcessor = new Thread(new ThreadStart(StartThread)); _threadAppProcessor.SetApartmentState(ApartmentState.STA); _threadAppProcessor.Start();