Solved

How to run multiple threads in VB???

Posted on 2003-11-17
7
1,072 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 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Progress
Introduction to Processes

623 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