Run 5 subrountines in parallel ( at the same time )

I have 5 subroutines in my Visual Basic 6 program. I would like to ask how can I run 5 subroutines at the same time ( in parallel ) but not run them 1 after the other ? now my situation is that :

call a()
call b()
call c()
call d()
call e()

the situation is that i run subrountine a() and then b() and then c() and then d() and then e(). If one of the subroutine get stuck, the whole program is freeze and doesn't run.

Would somebody suggest me how can I run the 5 subroutines at the same time in parallel ?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikekwokAuthor Commented:
Do you mean multi-threading in Visual Basic 6 can help me to solve the problem ? can u explain a little bit about the hyperlink ? Thanks.
Éric MoreauSenior .Net ConsultantCommented:
It is really hard to do multi-threading in VB6. Some workarounds include:
-Upgrade to VB.Net that fully supports multi-threading
-Create 5 VB6 applications that you will launch at the same time.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

You could cheat and organize code from subs to timers, which will execute one line at the time.

I know it's not elegant, and sometimes can be impossible, but It can be done.

Something like this:
Sub Timer_A.Timer ()
       CounterA = CounterA + 1
       Select Case CounterA
       Case 1
              'Execute first line of routine
       Case 2
              'Execute second line of routine
       Case 3
              'Execute third line of routine
       Case 4
              'Execute fourth line of routine
       Case 5
              Timer_A.Enabled = False                     'End if there's no more lines
       End Select
End Sub
Sub Timer_B.Timer ()
       CounterB = CounterB + 1
       Select Case CounterB
       Case 1
              'Execute first line of routine
       Case 2
              'Execute second line of routine
       Case 3
              'Execute third line of routine
       Case 4
              'Execute fourth line of routine
       Case 5
              Timer_B.Enabled = False                         'End if there's no more lines
       End Select
End Sub

Note that you have to declare every variable that is used by your subroutines  before activating timers becouse timer destroys its variables every time it finishes one 'tick', which
means that lines in one timer wont be able to comunicate with each other if you don't reserve variables for them.

Also note that loops, If - Then - Else - End If , Select Case  and similar statements have to be either put together (as one line).
mikekwokAuthor Commented:
Would you please tell me what is the advantage or disadvantage using ur method compare with the multi-threading method in Visual Basic 6 ?
Éric MoreauSenior .Net ConsultantCommented:
mikekwokAuthor Commented:
The timer method doesn't work for my situation. I still need to wait for the a() subroutine finsih before going into b() subroutine . Would somebody please help me ?
I think it may because I use do loop until in the a() subroutine.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
>> ... I would like to ask how can I run 5 subroutines at the same time ( in parallel ) but not run them 1 after the other ? ...

Then you aren't really asking how to run them in parallel are you? =)

If one of your subs gets "stuck" then you simply have bad logic in it.  Post the code and we can help you figure out where the problem is.

mikekwokAuthor Commented:
I get stuck because I have to wait all of the internet explorer finishing loading and then do something . however , sometimes one of the internet explorers keep loading and then my program get stuck. I would like to ask how can i run 5 subroutines at the same time, do you know how ? the timer method cannot help me solve the problem since i have used do until loop and doevents in the subroutines.
I agree with Idle_Mind. Post the code if you can, or at least explain what should each subroutine do, and why do you need them to run at the same time.
mikekwokAuthor Commented:
There are 5 subroutines in my program, let say : a(), b(),c(),d() and e() ( actually they are the command click subroutine). I use VB to open 5 internet explorers and when the user clicks on the button in the form, it will send some text to the internet explorer and then it will click submit button in a form in internet explorer. my situation is that after clicking the submit button in the internet explorer, it need time to finsih loading . and sometimes, it has error and keep loading. if the homepage keep loading after clicking submit button in internet explorer, my program will get stuck .... for example , my sequence is to run subroutine a() and then b() and then c() and then d() and then e(),  then if it is stuck in a() subroutine , then the whole program get stuck .

another situation is that i have to login 5 webpages using internet explorer object in visual basic. I want to login 5 homepage at the same time , instead of logging in the a homepage, then b homepage and then c homepage and then d homepage and at last e homepage. Would somebody suggest a solution for me , please?
Try using timer which will check if the page is loaded, and then run according sub when it is.

I don't know the actual syntax for that but here is some theory.

Use one sub which will send 5 requests to Internet Explorers. After that start the timer which will check every page and determine if it is loaded.
When it determines that page is loaded, run a sub which is assigned for that page.

For the second problem:
Try creating 5 internet explorer objects if possible, and use them as I described above.


Try creating 5 new Web Browser forms.   (  Add - Form - Web Browser )

Here is the example with only one webbrowser form:

Start a new project, add command button and a timer to form1. Set Timers enabled property to false
Add a Web Page form, and set its MDIChild property to false.

Paste this code to form1:
Private Sub Command1_Click()
    frmBrowser.cboAddress.AddItem ""
    frmBrowser.cboAddress.ListIndex = 0
    Timer1.Enabled = True
End Sub

Private Sub Form_Load()
End Sub

Private Sub Timer1_Timer()
    If frmBrowser.Caption = "Google" Then
        MsgBox "Page loaded"
        Timer1.Enabled = False
    End If
End Sub
mikekwokAuthor Commented:
Do you mean that I combine the 5 subroutine into 1 subroutine ?
mikekwokAuthor Commented:
I try to use Multi-threading by using createthread and it has error in the line i use ^^^^ to indicate :

It said : Automation Error : Coinitialize has not been called. would somebody pls help? Thanks.

Private Sub getbrowserobject(ByRef ie As Object, url As String)
    Dim shWin       As New ShellWindows
    Dim IE_loop          As InternetExplorer
    Wait 0.5
    On Error GoTo ErrorHandler

    For Each IE_loop In shWin

        Do While IE_loop.Busy
            'Wait 0.5
        If IE_loop.LocationURL = url Then
            Set ie = IE_loop
            Exit For
        End If
End Sub
Only put the parts which sends requests together. Leave everything else in your 5 subroutines.
As I did in my example. When clicked on command button, it sends request for openning, and starts a timer which checks if the page was loaded. (Forms caption becoms "Google")
After that, run the code you need. (I've put msgbox displaying some message)

If you put all code in the same sub then you have the same problem as before becouse program has to wait until each piece of code finishes before going to another.
To show you an example with 5 windows:

start new project
remove form1
add MDI form
set startup object to MDIform1
add 5 web browser forms
create 1 timer on MDI form
paste this code into MDIform1

Dim loaded(6) As Boolean
Private Sub MDIForm_Load()
    Load frmBrowser
    Load frmBrowser1
    Load frmBrowser2
    Load frmBrowser3
    Load frmBrowser4
    Call SendRequests
    Timer1.Interval = 1000
    Timer1.Enabled = True
End Sub

Sub SendRequests()
    frmBrowser.cboAddress.AddItem ""
    frmBrowser.cboAddress.ListIndex = 0
    frmBrowser1.cboAddress.AddItem ""
    frmBrowser1.cboAddress.ListIndex = 0
    frmBrowser2.cboAddress.AddItem ""
    frmBrowser2.cboAddress.ListIndex = 0
    frmBrowser3.cboAddress.AddItem ""
    frmBrowser3.cboAddress.ListIndex = 0
    frmBrowser4.cboAddress.AddItem ""
    frmBrowser4.cboAddress.ListIndex = 0

End Sub

Private Sub Timer1_Timer()
    If frmBrowser.Caption = "Google" Then
        MsgBox "1. page is loaded. Call Your a() function instead of me."
        loaded(1) = True
    End If
    If frmBrowser1.Caption = "Google" Then
        MsgBox "2. page is loaded. Call Your b() function instead of me."
        loaded(2) = True
    End If
    If frmBrowser2.Caption = "Google" Then
        MsgBox "3. page is loaded. Call Your c() function instead of me."
        loaded(3) = True
    End If
    If frmBrowser3.Caption = "Google" Then
        MsgBox "4. page is loaded. Call Your d() function instead of me."
        loaded(4) = True
    End If
    If frmBrowser4.Caption = "Google" Then
        MsgBox "5. page is loaded. Call Your e() function instead of me."
        loaded(5) = True
    End If

    If loaded(1) = True And loaded(2) = True And loaded(3) = True And loaded(4) = True And loaded(5) = True Then
        Timer1.Enabled = False
        MsgBox "Everything is loaded"
    End If
End Sub

I havent noticed that you have posted one more comment. This is not an answer to your last comment.
it would appear that it is the CALL part which is causing the probelm.. Passing a parameter bypassess this I think?

Therefore define your subs a()...e() as ....

Public sub a(sDummy as string)

End Sub....

Then have your routine...

a "junk"
b "junk"
c "junk"
d "junk"
e "junk"

see if something like that works...?
mikekwokAuthor Commented:
emoreau, u suggest me to create 5 VB6 applications and launch at the same time.
i would like to ask how to run the other 4 vb6 application from 1 vb6 application ?
i mean , i now have A application, b application, c application, d application and e application.
i don't how to run b, c ,d and e application from a application, also i don't how to pass parameter from a appliction to b,c,d and e application, would somebody pls help ?

Can i just like C++ run the b.exe and passing argument after typed b.exe ?
for example
in command prompt , i typed : b.exe 1 2 3 will run the b.exe and pass the value 1 , 2 and 3 into the b application.
Éric MoreauSenior .Net ConsultantCommented:
>>i don't how to run b, c ,d and e application from a application

See the Shell statement in the help file.

>>Can i just like C++ run the b.exe and passing argument after typed b.exe

Yes. Once again see the Shell statement.
mikekwokAuthor Commented:
Thanks. emoreau.
I would like to ask how can I make an VB6 Application that can read arguments in command prompt ? Thanks.
mikekwokAuthor Commented:
Thanks emoreau, i have found the command line argument myself :
mikekwokAuthor Commented:
Private Sub Form_Load()
If Command = "" Then
    Call Shell("C:\Program Files\Internet Explorer\iexplore.exe", vbNormalFocus)
    Call Shell("C:\Program Files\Internet Explorer\iexplore.exe " & Command, vbNormalFocus)
End If

End Sub
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.