Solved

Multithreading... maybe  -- Hard to explain

Posted on 2002-07-06
9
305 Views
Last Modified: 2010-05-02
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
Comment
Question by:samsonite1023
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 15

Expert Comment

by:ameba
ID: 7134556
> 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
 
LVL 6

Expert Comment

by:xSinbad
ID: 7134793
0
 
LVL 1

Author Comment

by:samsonite1023
ID: 7135452
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
Industry Leaders: 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!

 
LVL 15

Expert Comment

by:ameba
ID: 7135465
Than it's OK to use "Sleep 10"
0
 
LVL 1

Author Comment

by:samsonite1023
ID: 7135480
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
 
LVL 15

Expert Comment

by:ameba
ID: 7135578
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
 
LVL 18

Accepted Solution

by:
mdougan earned 100 total points
ID: 7138552
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
 
LVL 1

Author Comment

by:samsonite1023
ID: 7139385
Thanks for the info!
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7139460
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question