Solved

Run 5 subrountines in parallel ( at the same time )

Posted on 2003-11-24
24
346 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
ID: 9816361
0
 

Author Comment

by:mikekwok
ID: 9816392
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 70

Accepted Solution

by:
Éric Moreau earned 25 total points
ID: 9816871
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 11

Assisted Solution

by:dbrckovi
dbrckovi earned 25 total points
ID: 9817124
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
ID: 9822195
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 70

Expert Comment

by:Éric Moreau
ID: 9822253
0
 

Author Comment

by:mikekwok
ID: 9822337
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
ID: 9822561
>> ... 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
ID: 9822801
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
ID: 9822835
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
ID: 9822889
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
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9823139
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
ID: 9823210
Do you mean that I combine the 5 subroutine into 1 subroutine ?
0
 

Author Comment

by:mikekwok
ID: 9823223
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
ID: 9823247
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
ID: 9823313
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
ID: 9823324
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
ID: 9900498
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
ID: 9901532
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 70

Expert Comment

by:Éric Moreau
ID: 9901545
>>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
ID: 9901592
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
ID: 9901650
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
ID: 9901653
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

830 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