Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to run multiple threads in VB???

Posted on 2003-11-17
7
Medium Priority
?
1,076 Views
Last Modified: 2011-09-20
I want to run 5 subroutines at the same time. (in parallel)
any ideas on how to do this with VB6??

Thanks
0
Comment
Question by:davemiles
[X]
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
7 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 9766132
You can create ActiveX EXEs and call them.  They each would have their own timer.  Then you would start all 6.  If you need to know information back from them then give them events to talk back with your main app.
0
 

Author Comment

by:davemiles
ID: 9767015
ActiveX exe could work...
but i need to be able to use the mscomm control.

how can i use the control from the activex exe?

0
 
LVL 15

Accepted Solution

by:
SRigney earned 1000 total points
ID: 9767310
An ActiveX EXE is just like using a regular EXE.  You can add a form to your ActiveX EXE and use it there.  You can keep the form hidden so it doesn't create desktop clutter.
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 48

Expert Comment

by:Mikal613
ID: 9768616
you could (even though vb6 doesnt support it) but i would use Semaphores and Mutex's to tame your processes.
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9770273
"how can i use the control from the activex exe?"

take a form dont call the forms show method just keep it loaded and u can use the control.
you can load a form without making it visible using the load method
0
 
LVL 1

Expert Comment

by:RocketMan801
ID: 9771579
multi-threading under vb 6 is kinnda unestable, vb doenst like threads :) but u can do it using the CreateThread API.
for example:


'in a module:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long

Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public hThread As Long, hThreadID As Long

'the function i'm gonna use to launch the thread
Public Sub AThread()
    For i = 0 To 10
        Form1.Text1 = Form1.Text1 & "T2:" & i & vbCrLf
        Sleep (1000)
    Next i
End Sub

'in a form (form1) with 2 command buttons and a multinined textbox
Private Sub Command1_Click() 'click this button to start the thread
    hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID)
End Sub

Private Sub Command2_Click() 'click this to start a count, u will see it count while the thread is runnig,
                                              'so there are 2 counts at the same time
     For i = 0 To 10
        Form1.Text1 = Form1.Text1 & "T1:" & i & vbCrLf
        Sleep (1000)
        DoEvents
    Next i
End Sub

Private Sub Form_Unload(Cancel As Integer)
    CloseHandle hThread
    'If the thread is still running, close it
    If hThread <> 0 Then TerminateThread hThread, 0
End Sub

0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9776324
"but u can do it using the CreateThread API" This is for vb5 not vb6 vb 6 doesnt work consistantly with threads unless its an active x exe
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Make the most of your online learning experience.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

704 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