It seems that I really am not good with multithreaded applications. I am trying to open a FolderBrowserDialog
, but I was getting an exception telling me:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.
I have STAThreadAttribute
set in my Main method, but the FolderBrowserDialog is being called from a thread other than my main thread. I tried
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
but that gave the exception Failed to set the specified COM apartment state
.
I have a temporary fix that creates a new thread in STA mode and opens the FolderBrowserDialog
, but I would like to have a neater solution. What causes a failure to set the apartment state to STA?
Threading.ApartmentState.STA
when you creating any thread which access OLE related functions. – Trophoplasm