Solved

Run 5 subrountines in parallel ( at the same time )

Posted on 2003-11-24
24
343 Views
Last Modified: 2010-05-03
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.

0
Comment
Question by:mikekwok
  • 11
  • 6
  • 3
  • +3
24 Comments
 
LVL 3

Expert Comment

by:monkesdb
Comment Utility
0
 

Author Comment

by:mikekwok
Comment Utility
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.
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 25 total points
Comment Utility
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.
0
 
LVL 11

Assisted Solution

by:dbrckovi
dbrckovi earned 25 total points
Comment Utility
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).
0
 

Author Comment

by:mikekwok
Comment Utility
Would you please tell me what is the advantage or disadvantage using ur method compare with the multi-threading method in Visual Basic 6 ?
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
0
 

Author Comment

by:mikekwok
Comment Utility
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
>> ... 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
0
 

Author Comment

by:mikekwok
Comment Utility
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.
0
 
LVL 11

Expert Comment

by:dbrckovi
Comment Utility
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.
0
 

Author Comment

by:mikekwok
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:dbrckovi
Comment Utility
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
0
 

Author Comment

by:mikekwok
Comment Utility
Do you mean that I combine the 5 subroutine into 1 subroutine ?
0
 

Author Comment

by:mikekwok
Comment Utility
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
0
 
LVL 11

Expert Comment

by:dbrckovi
Comment Utility
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.
0
 
LVL 11

Expert Comment

by:dbrckovi
Comment Utility
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
--------------------------------------------------

0
 
LVL 11

Expert Comment

by:dbrckovi
Comment Utility
Sorry.
I havent noticed that you have posted one more comment. This is not an answer to your last comment.
0
 
LVL 1

Expert Comment

by:ictis
Comment Utility
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...?
0
 

Author Comment

by:mikekwok
Comment Utility
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.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
>>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.
0
 

Author Comment

by:mikekwok
Comment Utility
Thanks. emoreau.
I would like to ask how can I make an VB6 Application that can read arguments in command prompt ? Thanks.
0
 

Author Comment

by:mikekwok
Comment Utility
Thanks emoreau, i have found the command line argument myself :
http://www.vbexplorer.com/VBExplorer/vb_feature/august2000/command_line_arguments.asp
0
 

Author Comment

by:mikekwok
Comment Utility
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now