Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

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
0
samsonite1023
Asked:
samsonite1023
  • 3
  • 3
  • 2
  • +1
1 Solution
 
amebaCommented:
> 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).
0
 
samsonite1023Author Commented:
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
amebaCommented:
Than it's OK to use "Sleep 10"
0
 
samsonite1023Author Commented:
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?
0
 
amebaCommented:
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.
0
 
mdouganCommented:
Your initial thought of using multi-threading is correct.  You would really like each of these methods to run asynchronosly.  Sleep, by itself, will cause the executing thread to sleep for x number of milliseconds.  Since both of your instructions are executing in the same thread they will execute one after the other.

The timer control can sometimes work.  Consider it the poor man's way to multi-thread.  Timers will allow code to run asynchronosly, however timer events may not fire reliably (if the event queue gets backed up several timer events might not ever execute).

You really need to do true multi-threading.  Unfortunately, Microsoft changed the threading model under VB 6 so that it no longer supports the CreateThread API.  So, you have 3 choices.

Write your program in VB 5 and call the CreateThread API.

Write your program under VB.NET (I think they added true multi-threading to .NET).

Write your program in VB 6 and download one of the Multi-Threading OCX controls that are out there.  I've seen a few that cost money, but haven't seen any that are freeware.  Basically, these are OCX controls that were written in C or C++ which does support a threading model that allows for multi-threading.  By creating instances of the thread class in these controls, you can then execute your code in different threads.
0
 
samsonite1023Author Commented:
Thanks for the info!
0
 
mdouganCommented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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