Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all visitors,

I have added item in tool box name "Crystal ActiveX Report Viewer Control 10.5".
I dropped that control to my form. when i click on Button Print to display the report,
I got the error like that "An error occurred creating the form. See Exception.InnerException for details. The error is: ActiveX control '548c6316-751f-4b34-b8d9-a56fcee53bc8' cannot be instantiated because the current thread is not in a single-threaded apartment."

Does anybody know how to solve this problem?

Thanks

TONY
Posted

1 solution

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:
C#
[System.STAThread]
static void Main(string[] args) { /* ... */ }
or like this
C#
[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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900