Controlling Asynchronous Transfers

Execute() works asynchronously, and that means that we need some way of keeping track of what it is doing. In particular, we need to know when the operation is finished (such as when the file we want is downloaded). We do that in the Internet Transfer control's StateChanged event; double-clicking the Internet Transfer control opens this event:

Private Sub Inet1_StateChanged(ByVal State As Integer)
        End Sub

We can determine what is happening in this event by checking the variable passed to us: State. Table 3.2 lists the values that this variable can contain and describes what those values mean. Using this event handler, we can watch as the control connects to the Internet, downloads the data, and finishes.

Table 3.2 The Internet Transfer Control's StateChanged Values

Value Meaning
icNone (= 0) No state
icHostResolvingHost (= 1) Looking up IP address of specified computer
icHostResolved (= 2) Got the IP address of specified computer
icConnecting (= 3) Connecting to computer
icConnected (= 4) Connected to computer
icRequesting (= 5) Sending request to host computer
icRequestSent (= 6) Sent the request
icReceivingResponse (= 7) Receiving a response from computer
icResponseReceived (= 8) Received a response from computer
icDisconnecting (= 9) Disconnecting from computer
icDisconnected (= 10) Disconnected from computer
icError (= 11) Error occurred
icResponseCompleted (= 12) Request completed and data received

For example, if we want to determine when a particular document has been downloaded, we place code like this into the Inet1_StateChanged() event handler:

Private Sub Inet1_StateChanged(ByVal State As Integer)
   —>      If(State = icResponseCompleted) Then ReceivedFlag = True
        End Sub

In this way, we're able to determine when a document is fully downloaded when we use the Execute method. (There is no such question when you use the OpenURL method, because that method won't return until the file you have requested has been downloaded.)