CallbyName - waiting for the Thread to Return?

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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

CallByName "Sub1"

CallByName "Sub2"


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud9_UserAuthor Commented:
Yes, but it doesn't help :/

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
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.

Cloud9_UserAuthor Commented:
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 :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.