Solved

Worker threads in vb apartment activex dlls

Posted on 2000-02-21
9
356 Views
Last Modified: 2012-06-21
I want to create a worker thread that processes incoming messages to update a cache. However, I know that cross thread calls to activeX objects need to be marshalled between apartments - say if globals are used in the dll. However, I'm not creating another apartment, do I still need to marshal calls to objects on the apartment thread.
Basically I have a global collection object that is accessed by the apartment, and I want to create a worker thread that updates this collection as messages come in. If I use synchronisation objects on each update of the collection, I guess this will cause the apartment thread to hang while I block access to the collection - will vb be able to cope with this?
0
Comment
Question by:jimshort
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 2542008
> Whoosh...

> What was that?

> That was the sound of that question going straight over my head.

But I'm interested, so I'm posting a comment that will enable me to read the contents of this thread after it's been answered. I hope it will be answered satisfactorily.

I would be surprised if globals from one apartment were in any way accessible by other apartments, but as the above suggests, I know very little about threading so I can't state this with any certainty.

What exactly is a synchronisation object?
0
 
LVL 1

Expert Comment

by:DragonWolf
ID: 2542168
ditto
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2542201
I dont think it can be done. With apartment threading each thread has its own copy of the Global data, and there is no interaction between the two.

therefor if you update your collection in one thread, the "same" collection in the other thread would be unaffected.

You said in your question, that your not creating another apartment, but you can only have one thread per apartment, so where are you getting your second worker thread. Or like the other two has this question gone right over my head too.

Gordon
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:jimshort
ID: 2543453
ActiveX dll, has global ref to an object of type Cache which is externally exposed.

in class_initialise of Cache object have

set gCache = Me

(will need
public sub Cache_Clear
Set gCache = nothing
end sub

public sub Cache_Init
use createthread api call to create a thread, pass in function
Public sub ReadMsgQMessages
   Loop until get quit notification, getting messages. If message, call createmutex/createcriticalsection or something, do
gCache.Item(strKey).Dirty = True
destroymutex (or whatever synchronisation object I need)

There can only be one thread in an apartment, this is true, but does com know about this thread? Does windows prevent creating new threads once OLEInitialise has setup the main thread as apartment?

I know that if I create another thread and then call OLEInitialise - this creates an apartment in the com libraries, and any objects created using CoCreateInstance will be created on this thread - but I'm not doing this, I'm just spawning a thread that (in a supposedly thread safe manner) is accessing objects in an apartment - the com libraries know nothing about it - this is what worries me.

It is basically a hack to have 2 threads in an apartment, but before I go ahead and spend time doing it, I need to know if it's going to work :)
0
 
LVL 1

Expert Comment

by:GustavoValdes
ID: 2544375
You could do this using two projects, one in which you could store the data that both threads will share, but this project must be defined with a Thread Pool = 1, this will garantee that all the objects created within it will be in the main Single Threaded Apartment.
Then when you access this data from one thread or the other from your multithreaded program, you can be sure you're working with the same copy of the global data.
0
 
LVL 6

Expert Comment

by:VBGuru
ID: 2544901
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2545447
If you use CreateThread then Com does not know about it and it wont create an apartment.

However if you use VB6 then you cannot use CreateThread and in VB5 its unstable to say the least, but if you can make it work then Ok.

If you use seperate threads like this then use Mutex's will only block the thread rather than the apartment.

If you go with Gustavo's answer you'll still need to implement some form of synch.

Gordon
0
 

Author Comment

by:jimshort
ID: 2546018
Gustavo

I want it to be inprocess dll - there will be quite a bit of overhead on the cache access from the creator of the cache. The cache updator will not be calling that much though, so I can marshal across there. Thread pooling is used for out of process components. And indeed they will use the same global data as they are on the same thread, but that thread is out of process.

Gordon

I think mutex's/critical sections block sections of code from running, rather than any particular thread. Mutex's work cross process, whereas critical sections are with process.
The thing I need to do is follow com rules and marshall across the threads.

VBGuru, you have answered the question, the content on that page is excellent and confirms my worries about not marshalling the thread.
I think I'm going to do it in C++ using free threading with the freethreaded marshaller so my vb app can call it with no marshalling overhead. It is a fairly simple dll, so won't be too tricky to do in ATL

Thanks folks, - if you answer this VBGuru, you get the prize :)
0
 
LVL 6

Accepted Solution

by:
VBGuru earned 200 total points
ID: 2546230
Nice to know it helped you.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question