Link to home
Start Free TrialLog in
Avatar of mikekwok
mikekwokFlag for Hong Kong

asked on

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 ?
Thanks.

Avatar of monkesdb
monkesdb
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of mikekwok

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Would you please tell me what is the advantage or disadvantage using ur method compare with the multi-threading method in Visual Basic 6 ?
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.
>> ... 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.

Idle_Mind
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.
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?
Thanks
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.

Or:

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 "www.google.com"
    frmBrowser.cboAddress.ListIndex = 0
    Timer1.Enabled = True
End Sub

Private Sub Form_Load()
    frmBrowser.Show
End Sub

Private Sub Timer1_Timer()
    If frmBrowser.Caption = "Google" Then
        MsgBox "Page loaded"
        Timer1.Enabled = False
    End If
End Sub
Do you mean that I combine the 5 subroutine into 1 subroutine ?
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
ErrorHandler:
    On Error GoTo ErrorHandler





    For Each IE_loop In shWin
^^^^^^^^^^^^^^^^^^^^^^




        Do While IE_loop.Busy
            DoEvents
            'Wait 0.5
        Loop
        If IE_loop.LocationURL = url Then
            Set ie = IE_loop
            Exit For
        End If
    Next
End Sub
No.
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 www.google.com, 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 "www.google.com"
    frmBrowser.cboAddress.ListIndex = 0
   
    frmBrowser1.cboAddress.AddItem "www.google.com"
    frmBrowser1.cboAddress.ListIndex = 0
   
    frmBrowser2.cboAddress.AddItem "www.google.com"
    frmBrowser2.cboAddress.ListIndex = 0
   
    frmBrowser3.cboAddress.AddItem "www.google.com"
    frmBrowser3.cboAddress.ListIndex = 0
   
    frmBrowser4.cboAddress.AddItem "www.google.com"
    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
--------------------------------------------------

Sorry.
I havent noticed that you have posted one more comment. This is not an answer to your last comment.
Avatar of ictis
ictis

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...?
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.
>>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.
Thanks. emoreau.
I would like to ask how can I make an VB6 Application that can read arguments in command prompt ? Thanks.
Thanks emoreau, i have found the command line argument myself :
http://www.vbexplorer.com/VBExplorer/vb_feature/august2000/command_line_arguments.asp
Private Sub Form_Load()
If Command = "" Then
    Call Shell("C:\Program Files\Internet Explorer\iexplore.exe http://www.google.com", vbNormalFocus)
Else
    Call Shell("C:\Program Files\Internet Explorer\iexplore.exe " & Command, vbNormalFocus)
End If

End Sub