Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component

Index of Chilkat Blog Posts

September 7, 2007

FTP NumFilesAndDirs sometimes freezes my VB6 app?

I am having a problem with ftp.NumFilesAndDirs. Sometimes it freezes my VB6 app for a minute. This is how I use it:

          ftp.ListPattern = "*"
          For lFileIndex = 0 To ftp.NumFilesAndDirs - 1
          Next lFileIndex

I’ll explain the issue momentarily. But first I want to recommend a good defensive programming technique. Accessing an ActiveX property is actually a COM method call into the object which returns the property’s value. When using it in a "for" statement, you (probably) want to ensure the value is not changing, and you also don’t know how expensive it is to compute and return the value of the property. It’s always better to capture the property value in a local variable:

          ftp.ListPattern = "*"
          Dim fCount as Integer
          fCount = ftp.NumFilesAndDirs
          For lFileIndex = 0 To fCount - 1
          Next lFileIndex

Now to explain the NumFilesAndDirs behavior: The Chilkat FTP2 component caches the current remote directory listing internally (in-memory). Whenever information about it is requested, as long as the current remote directory hasn’t changed (via the ChangeRemoteDir method) or the ListPattern hasn’t changed, the cached information is used and no communication with the FTP server is necessary.

When the current remote directory is changed, or if ListPattern is modified, the next access to NumFilesAndDirs will cause the directory listing to be fetched from the FTP server. This is the delay. All subsequent access to NumFilesAndDirs, as long as nothing has changed, are fast and return the cached value. The same applies to methods such as GetFilename(index), GetSize(index), GetIsDirectory(index), etc.

The solution to keeping the user-interface responsive is to use the AbortCheck event. The AbortCheck event is called every N milliseconds according to the value of the HeartbeatMs property. (A HeartbeatMs value of 0 indicates that no AbortCheck events should be fired.)
The AbortCheck event / HeartbeatMs property is standard across all Chilkat components in all programming languages where events are possible: ActiveX, .NET, C++.

Here is a snippet in VB6 that shows how to use the AbortCheck event:

    ' Set our heartbeat interval to .1 seconds
    ' This causes ftp_AbortCheck to be called approximately every .1 seconds.
    ftp.HeartbeatMs = 100
' Called periodically according to the HeartbeatMs property.
Private Sub ftp_AbortCheck(abort As Long)
    ' You can always abort by setting the output-only abort argument to 1.
    If AbortButtonPressed Then
        abort = 1
    End If   
    ' Process user-interface events -- this is what keeps your user-interface responsive...
End Sub

In summary: When you call ChangeRemoteDir or set ListPattern, your next access to NumFilesAndDirs may require underlying communication with the FTP server. You can use the AbortCheck event to indicate "communications with FTP server in progress" and keep the UI responsive.

Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.