Exe server threading model - events from worker thread question 
Author Message
 Exe server threading model - events from worker thread question

Hello!

Actually I have two questions:

1: I'm writing a Free threaded exe server (_ATL_FREE_THREADED and
CoInitializeEx(multithreaded)). I've used the wizard to add Apartment
threaded ATL objects to this server. Will these objects be created in
separate apartments, or will a single STA be created containing all of
these objects?

2: The ATL objects mentioned above create worker threads which should
be able to fire events on the objects' event interfaces. I'm trying to
use hidden windows in the ATL objects and posting messages to those
windows from the worker threads.(I've successfully used this technique
in an in-process server, thanks to Alexander Nicholov!). One of the
arguments to RegisterClass (for creating the hidden window) is
hInstance. Can I use _Module.GetModuleInstance to get that handle? And
do my free threaded exe server have a message pump? I suppose the STA
(STAs?) containing the ATL objects must have message pump(s), can I use
them somehow? It seems like RegisterClass works, but my WndProc never
receives the messages posted from the worker threads!

Any explanations will be very appreciated!
Per Carlsson

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 16 Feb 2003 03:00:00 GMT  
 Exe server threading model - events from worker thread question
See inline.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================


Quote:
> Hello!

> Actually I have two questions:

> 1: I'm writing a Free threaded exe server (_ATL_FREE_THREADED and
> CoInitializeEx(multithreaded)). I've used the wizard to add Apartment
> threaded ATL objects to this server. Will these objects be created in
> separate apartments, or will a single STA be created containing all of
> these objects?

Not at all. Basically, you DON'T have any STA objects! You simply have
thread-unsafe MTA objects and they are doomed to cause you problems.
If you want STA objects, use the DECLARE_CLASSFACTORY_AUTO_THREAD
macro and change CComModule to CComAutoThreadModule in the
derivation of your CExeModule class. Then you'll have a thread pool of
STA threads where the objects using the auto-thread class factory will
be created. The threads are picked on the round-robin principle. The
thread count is decided at startup and is (processor_count * 4). You can
change that by overriding the default value for the last parameter of
CComAutoThreadModule::Init().

Quote:

> 2: The ATL objects mentioned above create worker threads which should
> be able to fire events on the objects' event interfaces. I'm trying to
> use hidden windows in the ATL objects and posting messages to those
> windows from the worker threads.(I've successfully used this technique
> in an in-process server, thanks to Alexander Nicholov!). One of the
> arguments to RegisterClass (for creating the hidden window) is
> hInstance. Can I use _Module.GetModuleInstance to get that handle? And

Yes.

Quote:
> do my free threaded exe server have a message pump? I suppose the STA
> (STAs?) containing the ATL objects must have message pump(s), can I use
> them somehow? It seems like RegisterClass works, but my WndProc never
> receives the messages posted from the worker threads!

The main thread has a message loop, but you never receive method calls
on it! Therefore, you've created your windows on temporary RPC threads
that are probably long since goen when you fire an event. This won't be
the case if you use the STA thread pool of course. Every thread in the
pool has its own message pump.
Quote:

> Any explanations will be very appreciated!
> Per Carlsson

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sun, 16 Feb 2003 03:00:00 GMT  
 Exe server threading model - events from worker thread question
Again, perfectly explained! :)

Many many thanks.

Quote:


> See inline.

> --
> =====================================
> Alexander Nickolov
> Microsoft MVP [VC], MCSD

> MVP VC FAQ: http://www.mvps.org/vcfaq
> =====================================



> > Hello!

> > Actually I have two questions:

> > 1: I'm writing a Free threaded exe server (_ATL_FREE_THREADED and
> > CoInitializeEx(multithreaded)). I've used the wizard to add
Apartment
> > threaded ATL objects to this server. Will these objects be created
in
> > separate apartments, or will a single STA be created containing all
of
> > these objects?

> Not at all. Basically, you DON'T have any STA objects! You simply have
> thread-unsafe MTA objects and they are doomed to cause you problems.
> If you want STA objects, use the DECLARE_CLASSFACTORY_AUTO_THREAD
> macro and change CComModule to CComAutoThreadModule in the
> derivation of your CExeModule class. Then you'll have a thread pool of
> STA threads where the objects using the auto-thread class factory will
> be created. The threads are picked on the round-robin principle. The
> thread count is decided at startup and is (processor_count * 4). You
can
> change that by overriding the default value for the last parameter of
> CComAutoThreadModule::Init().

> > 2: The ATL objects mentioned above create worker threads which
should
> > be able to fire events on the objects' event interfaces. I'm trying
to
> > use hidden windows in the ATL objects and posting messages to those
> > windows from the worker threads.(I've successfully used this
technique
> > in an in-process server, thanks to Alexander Nicholov!). One of the
> > arguments to RegisterClass (for creating the hidden window) is
> > hInstance. Can I use _Module.GetModuleInstance to get that handle?
And

> Yes.

> > do my free threaded exe server have a message pump? I suppose the
STA
> > (STAs?) containing the ATL objects must have message pump(s), can I
use
> > them somehow? It seems like RegisterClass works, but my WndProc
never
> > receives the messages posted from the worker threads!

> The main thread has a message loop, but you never receive method calls
> on it! Therefore, you've created your windows on temporary RPC threads
> that are probably long since goen when you fire an event. This won't
be
> the case if you use the STA thread pool of course. Every thread in the
> pool has its own message pump.

> > Any explanations will be very appreciated!
> > Per Carlsson

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.


Tue, 18 Feb 2003 13:41:49 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. COM Threading Model for ISAPI Worker Threads

2. Handling events from a worker thread in the main thread

3. what threading model is default when no Threading model key

4. ATL EXE and Worker threads

5. worker thread vs main thread?

6. Worker thread events using attributed ATL

7. Firing events from a worker thread

8. Firing events from worker threads

9. rasing COM event from worker thread

10. EXE Threading Model!

11. Connection Pionts Event firing in worker thread

12. ATL ActiveX Worker Thread to JavaScript Event Handler Not Work

 

 
Powered by phpBB® Forum Software