views:

389

answers:

2

I have to include a .NET application into another .NET application as a plugin. The plugin interface requires me to inherit from a template form. The form is then attached in a MDI when the plugin is loaded.

Everything is working so far, but whenever I register for drag and drop events, set the autocomplete mode for a combobox or at various other situations I get the following exception:

...the 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...

The main application is running in MTA and developed by another company, so there is nothing I can do about it.

I tried to do the things that cause these exceptions in STA threads, but that didn't solve the problem either.

Has anyone been in the same situation? Is there anything I can do to solve the problem?

+1  A: 

You could try to spawn new thread and call CoInitialize with 0 on it (aparment threaded) and run your application in this thread. However you won't be to update controls directly within this thread you should use Control.Invoke for every UI modification.

I don't know if this is going to work for sure, but you could try it.

devdimi
Thank you for your suggestion. I tried that but I got the same error when I invoke an UI element that was created by an MTA thread.
xsl
Can control how the application is started?Than you could wrap it in container app. Create thread, initialize COM and than call the Main method of the program.Depending on the application this may work or not work and crash the main program, but may be your last resort.(Before decompiling and changing the main application and that might be illegal.)You could also try to contact the company that developed it.
devdimi
A: 

Update: The company released a new STA version. The question is no longer relevant.

xsl