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
Solved

Multithreading... maybe  -- Hard to explain

Posted on 2002-07-06
9
297 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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.
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 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…

808 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