Some APIs require certain apartment state of the thread calling it: either STA or MTA.
Please see:
http://msdn.microsoft.com/en-us/library/system.threading.apartmentstate.aspx[
^].
There are two ways to solve such problems. Basically, here is the overview of the nature of the problem:
An application may require using two or more different API sets requiring certain apartment states. If they all require the same, the solution is simple: specify the apartment state using the special attribute for the application entry point. What to do if one API required STA and another requires MTA? The example is a WPF application, which requires STA but might need to use some API requiring MTA. In this case, you are bound to using them in different threads — this way you can specify different apartment states for different threads.
Let's start with the first situation. Look at your application entry point. It can look like this:
[System.STAThread]
static void Main(string[] args) { }
or like this
[System.MTAThread]
static void Main(string[] args) { }
(Signatures and return types may vary. Please see:
http://en.wikipedia.org/wiki/Main_function#C.23[
^].)
For some applications you can freely choose any of these two options, for some you cannot. For example, if this is a
System.Windows.Forms
application, you can choose ether one, but for WPF, only
STAThread
can be used. In both cases, you can run your Crystal ActiveX API in the same thread as the thread running
Main
, because it requires
STAThread
. Chances are, you can find a place where
STAThread
is set and turn it to
STAThread
. As I don't know where this is done, I cannot be 100% it would work.
So, let's consider more general case where you need to set the apartment thread state for a separate thread. This is done using the method
System.Threading.Thread.SetApartmentState
. The trick is: you cannot do it in the thread in question. You can only do it in another thread, before starting the thread.
Please see:
http://msdn.microsoft.com/en-us/library/system.threading.thread.setapartmentstate.aspx[
^].
That's it.
Good luck,
—SA