I'm trying to build a Winforms application with two COM components. However, one of the components only works when using [MTAThread]
and the other only works with [STAThread]
.
What would the recommended solution be?
I'm trying to build a Winforms application with two COM components. However, one of the components only works when using [MTAThread]
and the other only works with [STAThread]
.
What would the recommended solution be?
Windows forms requires [STAThread] to be present on it's main entry point. It will only work in Single threaded apartment state. You can use your STA COM object on the UI thread in Windows Forms, with no issues.
The typical approach for this is to create your own thread, and set the Thread.ApartmentState to MTA (although this is the default) for the separate thread. Initialize and use your MTA-Threaded COM components from within this thread.
ThreadStart threadEntryPoint = ...;
var thread = new Thread(threadEntryPoint);
thread.ApartmentState = ApartmentState.MTA; // set this before you call Start()!
thread.Start();