Single-threaded apartment?

  • Thread starter Thread starter Dustin Davis
  • Start date Start date
D

Dustin Davis

I've build an application that monitors a folder. When a new file is
created in that folder it kicks off a process. On such process is to
load a form. When I get to the statement to create and instance of the
form, I get this error message:

---------------------------
ActiveX control '243f57b7-dd96-4179-865d-27ac7b5df609' cannot be
instantiated because the current thread is not in a single-threaded
apartment.
---------------------------

The form is one that I've created and contains various active x
controls. It is on the initialization of these controls that it crashes.
How can I make this work?

Thanks,
Dustin
 
I think the form needs to be the primary thread. Can you create
the form and hide it, have it kick off the thread that monitors
the folder, and when that kicks back an event, show your form?

Robin S.
 
Dustin said:
I've build an application that monitors a folder. When a new file is
created in that folder it kicks off a process. On such process is to
load a form. When I get to the statement to create and instance of the
form, I get this error message:

---------------------------
ActiveX control '243f57b7-dd96-4179-865d-27ac7b5df609' cannot be
instantiated because the current thread is not in a single-threaded
apartment.
---------------------------

The form is one that I've created and contains various active x
controls. It is on the initialization of these controls that it crashes.
How can I make this work?

Thanks,
Dustin

Well, if this is a separate exe, then you need to make sure you have
the <STAThread> attribute on you sub main. The desinger normally does
this for you.

If you are creating this as a separate thread, then you need to
explicitly set that thread's ApartmentState property to
ApartmentState.STA - before you call it's start method :)
 
I think the problem is that everything is in the main form (frmMain).
This form kicks off the FileSystemWatcher, when the FileSystemWatcher
triggers the event, it is calling function also within frmMain.

Do I need to create a new routine that lanches frmMain and the
FileSystemWatcher separately? If that's the case it make take a while as
I will have to probably pull a lot of function out of frmMain and make
them public.
 
Dustin said:
I think the problem is that everything is in the main form (frmMain).
This form kicks off the FileSystemWatcher, when the FileSystemWatcher
triggers the event, it is calling function also within frmMain.

Do I need to create a new routine that lanches frmMain and the
FileSystemWatcher separately? If that's the case it make take a while as
I will have to probably pull a lot of function out of frmMain and make
them public.

Dustin - if the call is comming back from another thread, you need to
mashal the call to the method on the main form. You do that by calling
the method through the forms Invoke method. You can test if this is
required in the event by checking the Forms.InvokeRequired property...
 
Tom - I'm not sure I understand this:
you need to
mashal the call to the method on the main form. You do that by calling
the method through the forms Invoke method.

Can you give me more information on this?

Thanks,
Dustin
 
Dustin said:
Tom - I'm not sure I understand this:

Can you give me more information on this?

You have a couple of potential issues here.

1) your using AX controls that are expecting to be run on an sta
thread. This should be ok, as long as you access them on the same
thread they were created, because the IDE will make the main
application thread an sta thread by default (it will apply the
STAThread attribute to the sub main).

2) You can not safely access a windows forms control on any thread
other then the one on which it was created. That means you need to
mashal all access of the form or it's controls back to the main thread.
You can do this through the Invoke method that all controls inherit.
Here is a link to a fairly good series of articles on the topic:

http://msdn2.microsoft.com/en-us/library/ms951089.aspx

It's the code is in C# - but the principals are the same in VB as well.
It might be helpful if you were to post a minimal set of code, just
to show the basic architecture - then it would be easier to point out
exactly what changes to make :)
 
Back
Top