Multithreading... maybe  -- Hard to explain

Posted on 2002-07-06
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
  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:


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.

Question by:samsonite1023
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
LVL 15

Expert Comment

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
End Sub

Private Sub Timer1_Timer()
     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).

Author Comment

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?
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

LVL 15

Expert Comment

ID: 7135465
Than it's OK to use "Sleep 10"

Author Comment

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?
LVL 15

Expert Comment

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.
LVL 18

Accepted Solution

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.

Author Comment

ID: 7139385
Thanks for the info!
LVL 18

Expert Comment

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.

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

615 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