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
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
Here are some links that may help you with multi treading;
http://www.desaware.com/articles/threadingL3.htm
http://www.desaware.com/articles/Waitingl3.htm
http://planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=26900&lngWId=1
http://planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=24672&lngWId=1
http://planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=14479&lngWId=1
http://www.desaware.com/articles/threadingL3.htm
http://www.desaware.com/articles/Waitingl3.htm
http://planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=26900&lngWId=1
http://planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=24672&lngWId=1
http://planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=14479&lngWId=1
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"
ASKER
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?
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
> 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).