CallbyName - waiting for the Thread to Return?

Posted on 2004-11-08
Last Modified: 2008-02-01
Hey all,

Just a quick question about the threading that the Visual basic 6 function 'CallByName' uses.

I'm basically using the CallbyName to involke a number of different subroutines in a Class module. The problem is, that when i call a series of subroutines in a batch... i.e. Sub1, Sub2, Sub3 etc... The Sub routines are all getting mixed up because they are using the same variables which are changing before i call each routine.

The object of my question is to find out if there is any way to wait for the function that the CallbyName calls, too finish, before calling the next subroutine?

By that i mean something like the 'WaitForSingleObject' API that you'd use for processes etc.

Looking at all my debug infomation, it seems that the Class that is being called is running off in another thread, and instead of waiting for that thread to return, the calling routine is going off and getting the next routine straight after :/

What i don't want to do is add something at the end of each subroutine to signify its been finished (i.e. an event etc), its a lot of work and i'd rather just wait for the thread to finish and retun if there is a way?

Question by:Cloud9_User
    LVL 32

    Accepted Solution

    Have you tried issuing a DoEvents after each call to allow the sub to be processed?

    CallByName "Sub1"

    CallByName "Sub2"


    Author Comment

    Yes, but it doesn't help :/

    LVL 32

    Expert Comment

    I just did a test, and the code below runs synchronously.  i.e. each sub is called in turn.  Is this how you are calling the methods?

    Dim x As New Class1

    CallByName x, "Sub1", VbMethod
    CallByName x, "Sub2", VbMethod
    CallByName x, "Sub3", VbMethod
    LVL 10

    Expert Comment

    surely the whole point of threading a call is that you do not need to wait for the routine to finish before running the next routine... if each routine changes the same global variables you can neve be sure how they are going to be used - as you have discovered.  

    Workaround 1 create a set of variables for each routine..
    workareound 2 have a global flag that is set when a routine is passed the point where variables are required

    gbolFlag = false
    calbyname "Sub1"
    do while not gbolFlag
    callbyname "Sub2"

      somewhere in "Sub1" will be statement
    gbolFlag = true

    Workaround 3: do not thread calls and just call routines sequentially.


    Author Comment

    Hmm.... it doesn't matter now anyways, i fixed the issue :)

    As for the thread, that is basically how the thread is running but because i was setting a single modular variable before running the CallbyName, by the time the First Function was half way through that modular variable had changed to the last number... i.e. the CallbyName was finishing the loop (of, for example, three subrountines) before the first sub had finished.

    Too me this says that the CallByName is returning the Thread before the thread has finished :/

    I fixed it anyways, just in case anyone is looking for ideas, of basically dimming a new instance of the Class each time, after i create a new instance i take a TickCount of the time of Creation and then check for Threads that go beyond 60 seconds in a timer.

    I'll give you the points however, seeing as your the only one who came to my rescue :)


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    755 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

    22 Experts available now in Live!

    Get 1:1 Help Now