Dear John, How Do I... Load a Property Asynchronously?

ActiveX controls used on Web pages may need to load property settings asynchronously. This allows the user's Web browser to display the contents of a Web page while transferring graphics or other large pieces of data in the background.

Figure 6-11 shows an Asynchronous Animation control based on the Animation control found in the Microsoft Windows Common Controls-2 6.0 (MSCOMCT2.OCX).

Figure 6-11. An Asynchronous Animation control playing an Audio Video Interleaved (AVI) file that was downloaded in the background.

The Asynchronous Animation control's AVIFile property takes a string argument that can be a local file specification or a Uniform Resource Locator (URL). The AsyncRead method begins the transfer of the file to the local machine, saving the file in the Windows Temp directory with a name generated by Visual Basic. The following code shows the AVIFile property:

Option Explicit

Dim mstrAVISourceFile As String
Dim mstrTempAVIFile As String

`~~~.AVIFile
Property Let AVIFile(Setting As String)
    If UserControl.Ambient.UserMode _
        And Len(Setting) Then
        AsyncRead Setting, vbAsyncTypeFile, "AVIFile"
        mstrAVISourceFile = Setting
    End If
End Property

Property Get AVIFile() As String
    AVIFile = mstrAVISourceFile
End Property

The AsyncRead method triggers the AsyncReadComplete event when the transfer is complete. The AsyncReadComplete event procedure is a general handler that runs for all asynchronous events. Use the AsyncProp.PropertyName value in a Select Case statement to execute specific code for each asynchronous property in your control. The AsyncReadComplete event procedure for the sample control opens and plays the AVI file by using an Animation control named aniControl, as shown here:

`General event handler for all async read complete events
Private Sub UserControl_AsyncReadComplete _
    (AsyncProp As AsyncProperty)
    Select Case AsyncProp.PropertyName
        `For AVIFile property
        Case "AVIFile"
            `Store temporary filename
            mstrTempAVIFile = AsyncProp.Value
            `Open file
            aniControl.Open mstrTempAVIFile
            `Play animation
            aniControl.Play
        Case Else
    End Select
End Sub

When the control terminates, be sure to clean up any temporary files you created. Well-behaved Internet applications should not fill up the user's disk with unneeded temporary files. The following code closes the AVI file and then deletes the temporary file containing the data:

Private Sub UserControl_Terminate()
    `Delete temporary file
    If Len(mstrTempAVIFile) Then
        aniControl.Close
        Kill mstrTempAVIFile
    End If
End Sub

To use the Asynchronous Animation control, simply draw the control on a form and set the AVIFile property from an event procedure. For example, the following code uses an Asynchronous Animation control named aaniFindFile to load the Find File animation from the Visual Studio CD-ROM:

Private Sub Form_Load()
    aaniFindFile.AVIFile = _
        "d:\common\graphics\avis\findfile.avi"
End Sub

SEE ALSO