Open a folderbrowser from a thread - possible?

  • Thread starter Thread starter Andre Nogueira
  • Start date Start date
A

Andre Nogueira

Hi guys,

Since you helped me out the last time I needed - let me thank you for that
if I haven't already ;-) - I decided to call for your help yet again.

I'd like to be able to open a FolderBrowser instance from within a thread I
lauched, but I don't seem to be able to. It gives me this exception

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.

And points me to this MSDN page

http://msdn2.microsoft.com/en-us/library/ms241609.aspx

So... Is there any way I can accomplish this, or do I have to think of
another way?

Thanks!

Andre Nogueira
 
Andre,
I'd like to be able to open a FolderBrowser instance from within a thread I
lauched
Why?


So... Is there any way I can accomplish this, or do I have to think of
another way?

It can certainly be done but you have to make the thread to run in STA
like the message says, bu setting its ApartmentState property.


Mattias
 
Hi,

Thank you for your reply.

The reason I need to open if from a new thread, is that my application is
plugin-enabled. When the user loads/enables a plugin, my application calls
the "SetUp()" function on the plugin - and this might envolve ask the user
to select a folder, show a windows form, and so on.
A workaround would be to call this function from within my program (ie
without actually opening a new thread)... But in the meantime I'll try to
solve this, if only to gain new knowledge :-)

I ended up doing what the message says and it did work, but now I get this
error message:

The CLR has been unable to transition from COM context 0x1b07f8 to COM
context 0x1b0a20 for 60 seconds. The thread that owns the destination
context/apartment is most likely either doing a non pumping wait or
processing a very long running operation without pumping Windows messages.
This situation generally has a negative performance impact and may even lead
to the application becoming non responsive or memory usage accumulating
continually over time. To avoid this problem, all single threaded apartment
(STA) threads should use pumping wait primitives (such as
CoWaitForMultipleHandles) and routinely pump messages during long running
operations.

I tried to Google it, but couldn't find anything I could use. I'll try to
search a bit more before I change my code to call the setup() function
before creating a thread...

Andre Nogueira
 
Back
Top