Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Multiple Download Inet1

I am trying to download a number of items simultaneously.

I have 2 Inet control on my form (Inet1(0) and Inet1(1)) and this is a subset of the code I am using:

dim item1 as string
dim item2 as string

Inet1(0).RequestTimeout = 10
Inet1(1).RequestTimeout = 10
DoEvents
item1 = myform.Inet1(0).OpenURL("http://www.google.com")
item2 = myform.Inet1(1).OpenURL("http://www.experts-exchange.com")

The problem is that it performs item1 before doing item2, how do I make the program do it simultaneously?
0
cMh
Asked:
cMh
  • 4
  • 3
1 Solution
 
saurabhguptaCommented:
Hi cMh,
Use the Execute method method to make asynchronous call
Inet1.Execute URL, "GET"

Private Sub Inet1_StateChanged(ByVal State As Integer)
    Select Case State
    Case icResponseCompleted
    On Error Resume Next
    Dim tmpData As String
    tmpData = Inet1.GetChunk(1024, icString)
    While Len(tmpData) <> 0
        DoEvents
        HTML = HTML + tmpData
        tmpData = Inet1.GetChunk(1024, icString)
    Wend
    Me.Hide
    Case icError
    End Select
End Sub

Regards
Saurabh
0
 
cMhAuthor Commented:
Thanks for the quick reply...

What is the _StateChanged sub for?
0
 
saurabhguptaCommented:
cMh,
> What is the _StateChanged sub for?
This event is fired in case of asynchronous calls to report the current state of the request.

Private Sub Inet1_StateChanged(ByVal State As Integer)
    Select Case State
    Case icResolvingHost
    Case icConnecting
    Case icConnected
    Case icRequestSent
    Case icReceivingResponse
    Case icDisconnecting

    Case icResponseCompleted
    On Error Resume Next
    Dim tmpData As String
   Dim MyData As String  
  tmpData = Inet1.GetChunk(1024, icString)
    While Len(tmpData) <> 0
        DoEvents
        MyData = MyData + tmpData
        tmpData = Inet1.GetChunk(1024, icString)
    Wend
 'process MyData

    Case icError
 'handle error
    End Select
End Sub


Regards
Saurabh
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
cMhAuthor Commented:
I haven't tested this yet but assuming it works, how do I make the program pause until all of the information from both Inet controls is collected?
0
 
saurabhguptaCommented:
Option Explicit

Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Private Const WAIT_FAILED = &HFFFFFFFF       'Our WaitForSingleObject failed to wait and returned -1
Private Const WAIT_OBJECT_0 = &H0&           'The waitable object got signaled '
Private Const WAIT_ABANDONED = &H80&         'We got out of the waitable object
Private Const WAIT_TIMEOUT = &H102&          'the interval we used, timed out.

'Message constants
Private Const QS_HOTKEY = &H80
Private Const QS_KEY = &H1
Private Const QS_MOUSEBUTTON = &H4
Private Const QS_MOUSEMOVE = &H2
Private Const QS_PAINT = &H20
Private Const QS_POSTMESSAGE = &H8
Private Const QS_TIMER = &H10
Private Const QS_MOUSE = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT = (QS_MOUSE Or QS_KEY)
Private Const QS_ALLEVENTS = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)

Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long

Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (lpEventAttributes As SECURITY_ATTRIBUTES, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
Private Declare Function SetEvent Lib "kernel32" (ByVal hEvent As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private hEvent(1) As Long
Private MyData(1) As String

Private Sub Command1_Click()
    Inet1(0).Execute "http://www.abc.com/", "GET"
    Inet1(1).Execute "http://www.xyz.com/", "GET"
    'Wait for 10 seconds
    MsgWait 10
    MsgBox MyData(0) & MyData(1)
    'process MyData(0) & MyData(1)
End Sub

Private Sub Inet1_StateChanged(Index As Integer, ByVal State As Integer)
    Select Case State

    Case icResponseCompleted
    On Error Resume Next
    Dim tmpData As String
    tmpData = Inet1(Index).GetChunk(1024, icString)
    While Len(tmpData) <> 0
        DoEvents
        MyData(Index) = MyData(Index) + tmpData
        tmpData = Inet1(Index).GetChunk(1024, icString)
    Wend
    'set event
    SetEvent hEvent(Index)

    'handle error
    Case icError
    SetEvent hEvent(Index)
    End Select
End Sub

'This routine waits for a specified time without blocking events
Private Function MsgWait(ByVal lngSec As Integer) As Long
    'This code creates an event
    Dim sd As SECURITY_ATTRIBUTES
    With sd
       .nLength = Len(sd)       'we pass the length of sd
       .lpSecurityDescriptor = 0
       .bInheritHandle = 0
    End With
    hEvent(0) = CreateEvent(sd, False, False, "MyEventNotification1")
    hEvent(1) = CreateEvent(sd, False, False, "MyEventNotification2")
   
   
    Dim WaitRet As Long
    Dim StartTime As Long
    '==Start wait loop==
    StartTime = CLng(Timer)
    Do Until Timer > StartTime + lngSec
        WaitRet = MsgWaitForMultipleObjects(2, hEvent(0), False, 100, QS_ALLEVENTS)
       
        Select Case WaitRet
        Case WAIT_TIMEOUT       'The first case must always be WAIT_TIMEOUT 'cause it is the most used option
            DoEvents            'until the shelled process terminates
        Case WAIT_FAILED Or WAIT_ABANDONED
            MsgBox "Wait failed or abandoned", vbExclamation
            Exit Do
        Case WAIT_OBJECT_0      'The first event got signaled so get out of the loop
            Exit Do
        Case WAIT_OBJECT_0 + 1  'The second event got signaled so get out of the loop
            Exit Do
        Case WAIT_OBJECT_0 + 2  'Other event
            DoEvents
        Case Else
            Debug.Assert False
        End Select
    Loop
    '==End wait loop==
   
    'Close event handle
    CloseHandle hEvent(0)
    CloseHandle hEvent(1)
   
    hEvent(0) = 0
    hEvent(1) = 0
    MsgWait = WaitRet
End Function

0
 
saurabhguptaCommented:
This code will make 2 asnync requests and wait for both them to complete or 10 seconds, whichever is earlier. If you have difficulty understanding the code let me know.
I have used MsgWaitForMultipleObjects instead of Sleep API so that the events are not blocked.

Regards
Saurabh
0
 
Richie_SimonettiIT OperationsCommented:
hearing...
0
 
cMhAuthor Commented:
Thanks for all your help saurabhgupta!  This works great.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now