Link to home
Start Free TrialLog in
Avatar of samsonite1023
samsonite1023

asked on

Multithreading... maybe -- Hard to explain

I don't know what I need, but here is my current problem:

I would like to make an object (ActiveX or user control, or whatever) that has a method which loops for a few seconds, such as:

private sub methodname()

for i = 1 to 100
  'code
  doevents
  sleep 10
next i

end sub




This works fine.  However, if I put 2 of these objects on a form (call them object1 and object2) and run this code:

object1.methodname
object2.methodname

Visual Basic waits for object1 to finish running the sub "methodname" before moving on to object2

How can I get them to run their procedures at the same time?

If this is hard to understand, please let me know what else you would like me to include.


Thanks,
-Sam
Avatar of ameba
ameba
Flag of Croatia image

> object1.methodname
> object2.methodname

If you have those two line in one procedure, first will run the first procedure, then after it has finished, the second.

Start your task(s) asynchronously, e.g. here is a simple way using a timer (Interval = 1):

Private Sub cmdStart()
     ' create objects
     ' ...
     Timer1.Enabled = True ' this will start the first task
     object2.methodname
End Sub

Private Sub Timer1_Timer()
     object1.methodname
     Timer1.Enabled = False
End Sub

> sleep 10
This will Not give more time to the second task in the same process, but to other applications and to Windows.
To run your tasks smoothly, add enough DoEvents lines (e.g. 10 DoEvents per second).
Avatar of samsonite1023

ASKER

The "Sleep 10" command is there because the loop will be performing a visual animation with the control (moving it, fading it).  Is there a better way to do this?
Than it's OK to use "Sleep 10"
I just tried out your suggested code, the same thing happens.  It seems like once it gets into the loop, everything stops until it gets done it.

Any other things I could try?
Maybe your loop period lasts too long, or there isn't enough DoEvents.
I know it works.  The other things, like threading are maybe too complex.

If you want, I can create sample, e.g. two long calculations running at the same time, and each updating one label on the form.
ASKER CERTIFIED SOLUTION
Avatar of mdougan
mdougan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the info!
Thanks.  Since my last post, I've downloaded a sample multi-treading vb program using CreateThread that didn't crash under VB6.... this is very odd, because I'd had a sample that crashed under VB6 every time, but worked fine under VB5, using the same API.  Now, I know it was crashing under W2K, and here I'm testing on NT 4.0, so, I don't know if that makes a difference or not.

If you are interested in a sample to try, let me know.  It would help if you posted an e-mail address, and I can forward a zip of a project.