Question about threading model options in VB 6.0

Posted on 2005-04-20
Last Modified: 2013-12-04

I am programming a VB 6.0 application and I am trying to understand the threading options that are available.

Under the project properties I have a number of options:

(1) Apartment Threaded, (2) Single threaded,  

There are also check boxes for
a) Unattended execution
b) Retained in Memory
c) Upgrade ActiveX controls

This is compiled as an ActiveX dll and I have chosen the "Apartment Threaded Option"

Does this mean that a separate instance of the ActiveX dll memory will be created for each thread that instantiates the object via a "CreateObject" call?  The .dll will be running on one or more multithreaded processes, and each process thread may instantiate one or more of the objects.


Q1) AM I at risk for a collision and threads walking on each other on the Class Module, or am I protected by the apartment threading properties of having separate data for each thread.

Q2) Am I protected in the module sections of the .dll? The reason I ask this, is that the public variables in the module section appear to be persistent between threads. Is there anything like an "EnterCriticalSection" (similar to  C++) to protect against reentrancy in the module if this is critical?

Q3) Let's say this COM .dll is created by 4 threads in one process and 3 in another: Do I then have 7 separate instantiations of this object?

q4)  Are the module public sections across processes at risk of colliding?

I know this is a complex question, so I am offering 500 points to start. For a great answer, I'll give more!


Question by:cschene
    LVL 10

    Expert Comment

    by:Eduard Ghergu
    "You must turn on the Unattended Execution option and the Retained In Memory option before compilation to host a Microsoft Visual Basic Enterprise Edition for Windows 6.0 component (Microsoft ActiveX DLL) in a multi-threaded environment, such as Microsoft Transaction Server (Mtx.exe), Internet Information Services (Inetinfo.exe), and COM+ (Dllhost.exe)":

     = In conclusion, you have to check these check-boxes only if you plan to use Microsoft Transaction Server (MTS) or COM + in order to create an enterprise level application. =

    "When you start a new project the currently registered controls are the only ones available. If you start a VB5 project when the VB6 controls are registered, it will require the VB6 versions of the controls to run. The VB compiler allows you to set an existing project to not upgrade ActiveX controls. If you do not check this option, existing projects will be converted to use the VB6 controls. " :

     = In conclusion, you have to check this check-boxe only if you are using VB 5.0. =

    "Single Threaded forces your ActiveX DLL to use a single thread for all its objects, even if the client is multithreaded. If you use Single Threaded on a Multithreaded client, the performance of your application will be degraded due to cross-thread marshaling, which is discussed later in this section. "
    "Selecting Apartment Threaded will not guarantee that your ActiveX DLL will use multiple threads. If the client application is single-threaded, then all objects of the ActiveX DLL will be attached to a single thread. If the client application is multithreaded, objects of the ActiveX DLL will be distributed and attached to multiple threads accordingly. Reentrancy rules should be strictly adhered to using this threading model with an ActiveX DLL. "

    CreateObject call is used only for late-binding purposes. If you want performance, use "new" operator(ealy-binding).

    Q1) What collisions are you affraid from? You wil won't get any collisions between threads when using Apartment model.

    Q2) Modules are available (and has visibility) to the whole application. So, you cannot protect them. Try to use classes and define the variables as members.

    Q3) Not necessarily. That will depend on COM runtime libraries.

    Q4) Yes, if can override very easy a value of a variable declared in a module. (see Answer to Q2).

    I hope that answer will help you. If you have more question, please ask.

    Author Comment


    Thanks for the reply...I need to read the reerences. I'll probably have more questions.


    Author Comment

    >You must turn on the Unattended Execution option and the Retained In Memory option before compilation to host a >Microsoft Visual Basic Enterprise Edition for Windows 6.0 component (Microsoft ActiveX DLL) in a multi-threaded environ

    Ok, Are you saying that if I do not check those two boxes that even though I have specified "Aprtment threading" I am still, in effect, single threaded? The program that is invoking my dll IS multithreaded.

    >= In conclusion, you have to check these check-boxes only if you plan to use Microsoft Transaction Server (MTS) or COM + >in order to create an enterprise level application. =
     I am not using those applications, but I am runing in a process control enviroment where I must process parallel transactions through one or more servers. In particular I have a historian server (low priority but lots of data), and hi priority real time process I think the same principle applies.

    Q2) Modules are available (and has visibility) to the whole application. So, you cannot protect them. Try to use classes and define the variables as members.

    Are modules the same within a given process?  I believe I can protect them, if need be, using a semaphore to prevent re-entracy. I believe I could even call a C/C++ .dll from VB if some feature to accomplish the "re-entracy blocking" were not available in VB.  I'd rather not, but this application is extremely critical (runs oil refineries and chemical plants)

    LVL 10

    Accepted Solution

    The Apartment model is the best solution for a multithreading COM application.
    What kind of transactions do you have to process? Do you have a database behind ? In that case, a good solution is to use, though, the transaction server (MTS) or COM+ if you have Win2K or newer. You have no semaphore mechanism in VB. Also, it's a good ideea to use a C/C++ dll(s) for critical part of application.

    There are some changes in COM+ comparing with the old COM. Please have also a look at:

    Author Comment

    "What kind of transactions do you have to process? "

    I am processing alarming and process variable requests for a DCS (Distributed Control System) process control system.  The DCS systems enable the operators to view and control the plant or process via graphical pictures that represent the plant. These displays are connected to what is, in essence, a real time data base that is typically distributed over as many as 100 or more computers. The displays show the information from this real time database and report system anomalies in the form of  alarms.

    These systems are usually highly redundant for the most dangerous processes and will often run  for years without ever being shutdown.  I have upgraded systems where a given node has been running for 3 years or more without ever being shutdown.

    When  software is upgraded, for example, one node in a redundant pair will be shutdown and reloaded but the process is still running on the alternate node while the new software is loaded. Once the new load is complete and the data bases synchronized, the other node in the pair is shutdown and reloaded. When this second node is shutdown, the process again fails over to the redundant partner so that the process control is never interrupted during the software upgrade.

    A process variable would be something like temperature, pressure, flow rate, tank level, etc. The DCS systems have very stringent alarming and alarm reporting requirements and I am processing information that aggregates alarm and process information and very quickly points the operator/engineer to the source of a problem so they can correct it quickly.

    For a large continuous process (oil refinery, chemical plants, etc) a problem that goes uncorrected for even a few minutes can result in a  product, such as premium grade gas, being out of spec for hours or the entire day.

    LVL 10

    Expert Comment

    by:Eduard Ghergu
    Thank you for your answer. If you still have any questions, I'll be happy to help you. Thank you, also, for the expert points.

    Author Comment

    You are welcome!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    This article will show, step by step, how to integrate R code into a R Sweave document
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now