Solved

Worker threads in vb apartment activex dlls

Posted on 2000-02-21
9
354 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now