DLL & Single-Threaded Apartment Issue

S

SR

I have written a DLL to give to a customer that he can use to search a
database and bring back some results. When he calls my search function,
once the search is performed a windows form is opened up with the results.

This functionality works fine if called from a windows form but throws a
theading error if called from a console application and it definitely has
something to do with the fact that I am opening up a form because other
function calls execute fine.

This is the error:

ActiveX control '<GUID>' cannot be instantiated because the current thread
is not in a single-threaded apartment.

Is the compiler setting my form as an ActiveX control or is it having a
problem with a control on my form? The controls I have on my form are a
TreeView, TabControl, ToolStrip, WebBrowser, ToolStripStatusLabel, and a
RichTextBox.

I am calling a webserivce function for my search, which works fine, but when
I create a new form object, that's when the error occurs. I figure I
probably just need to add a parameter or another line of code to allow the
form to be created and shown, but I don't know what that line or parameter
might be!

Can anyone tell me what I need to do to solve this issue?

TIA

SR
 
A

Alvin Bruney [MVP]

I'm a bit puzzled here. What exactly does your dll do? I know it searches
but where do the forms come from - is it in your dll or is it coming from
somewhere else? You are running a console application so where are the
"forms" coming from? If you already have a web service, what's the point of
a dll that calls the webservice. A web service is itself a callable
interface. Most likely you need to set the threading model in the console
application to STA but I really see a problem in the design.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
 
R

Rick Strahl [MVP]

Uhm why would you use a Console project if you are opening a Windows form?

IAC, I think the issue is that you don't have the [STAThread] on your
mainline static entry point:

[STAThread]
static void Main(string[] args)
{
...
}

Additionally unless you're invoking the Windows Form as a dialog, (ie.
ShowDialog) you'll run into all sorts of issues because there will be no
message loop. That may also be the reason your form is not working...

+++ Rick ---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top