• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

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?

0
Cloud9_User
Asked:
Cloud9_User
  • 2
  • 2
1 Solution
 
Erick37Commented:
Have you tried issuing a DoEvents after each call to allow the sub to be processed?

CallByName "Sub1"
DoEvents

CallByName "Sub2"
DoEvents

etc.
0
 
Cloud9_UserAuthor Commented:
Yes, but it doesn't help :/

0
 
Erick37Commented:
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
0
 
RichardCorrieCommented:
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
eg

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

  somewhere in "Sub1" will be statement
gbolFlag = true

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

/Richard
0
 
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 :)

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now