Solved

How to run multiple threads in VB???

Posted on 2003-11-17
7
1,069 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
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 250 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

776 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