Solved

Multithreading... maybe  -- Hard to explain

Posted on 2002-07-06
9
271 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
Comment Utility
> 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
Comment Utility
0
 
LVL 1

Author Comment

by:samsonite1023
Comment Utility
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
 
LVL 15

Expert Comment

by:ameba
Comment Utility
Than it's OK to use "Sleep 10"
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:samsonite1023
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the info!
0
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now