Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
FTP Heartbeat Events, Progress Monitoring, and Abort
VB.NET FTP sample program demonstrating progress monitoring event and abort capabilities. ' Chilkat FTP2 .NET component provides for both synchronous and asynchronous uploads
' and downloads. In both cases, data transfers can be monitored
' and aborted. This example discusses synchronous FTP operations.
'
' All FTP2 methods having the potential to block (i.e. not return immediately)
' will fire heartbeat events periodically. The HeartbeatMs property is used
' to set the heartbeat interval. Setting it to 0 indicates that no heartbeat
' events should be fired.
'
' At each heartbeat interval, the AbortCheck event is fired. Your application
' may set an abort flag to cause the current FTP operation to abort. AbortCheck
' events are fired during all FTP operations: connect, authentication, SSL/TLS
' channel establishment, data connection setup, data transfers, disconnect, etc.
' All FTP operations are abortable.
'
' In addition to the heartbeat, methods that transfer data (PutFile, GetFile, etc.)
' will fire PercentDone events. Each time the percentage completion increases,
' a PercentDone event is fired with the new percentage completion value.
' (It is also possible to abort a data transfer from a PercentDone event.)
'
' When a data transfer is in progress, the UploadRate and DownloadRate properties
' will contain real-time values for the data transfer rate (in bytes per second).
' This example demonstrates a synchronous FTP upload with progress monitoring.
' Progress monitoring is identical for both secure and non-secure FTP operations.
' Don't forget to use WithEvents!
Dim WithEvents ftpObj As New Chilkat.Ftp2
Dim AbortButtonPressed As Boolean
' This is the AbortCheck event callback.
Private Sub ftpObj_OnAbortCheck(ByVal sender As Object, ByVal args As Chilkat.AbortCheckEventArgs) Handles ftpObj.OnAbortCheck
' Was the Abort button pressed?
If (AbortButtonPressed) Then
' To abort the current FTP operation, set Abort = true
args.Abort = True
End If
' Update a progress bar so we can visually see the heartbeat:
If (ProgressBar1.Value < 100) Then
ProgressBar1.Value = ProgressBar1.Value + 10
Else
ProgressBar1.Value = 0
End If
' Handle application events so our user interface remains responsive.
Application.DoEvents()
End Sub
' Our user-interface has an abort button which when pressed,
' sets the AbortCurrentFtpOp boolean to True. If an FTP operation
' is in progress, it will be aborted at the next heartbeat.
Private Sub AbortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbortButton.Click
AbortButtonPressed = True
End Sub
' This is the percent-done event callback. Update another progress bar with
' the percentage done value
Private Sub ftpObj_OnFtpPercentDone(ByVal sender As Object, ByVal args As Chilkat.FtpPercentDoneEventArgs) Handles ftpObj.OnFtpPercentDone
ProgressBar2.Value = args.PercentDone
End Sub
' Connect to an FTP server and upload a file. Both heartbeat and
' percent-done events will fire.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
AbortButtonPressed = False
Dim success As Boolean
success = ftpObj.UnlockComponent("Anything for 30-day trial")
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
' -----------------------------------------------
' IMPORTANT: Be sure to enable events on the object
' -----------------------------------------------
ftpObj.EnableEvents = True
' Set our heartbeat interval to .1 seconds:
ftpObj.HeartbeatMs = 100
' Set the FTP hostname, login, and password.
ftpObj.Hostname = "ftp.yourftpserver.com"
ftpObj.Username = "***"
ftpObj.Password = "***"
' Connect and login to the FTP server. Heartbeat events fire during the
' the connection establishment and authentication.
success = ftpObj.Connect()
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
' Upload a file.
Dim localFilename As String
Dim remoteFilename As String
localFilename = "hamlet.xml"
remoteFilename = "hamlet.xml"
success = ftpObj.PutFile(localFilename, remoteFilename)
If (Not success) Then
MessageBox.Show(ftpObj.LastErrorText)
Exit Sub
End If
' Disconnect
ftpObj.Disconnect()
' We're finished!
MessageBox.Show("FTP Upload Finished!")
End Sub
|
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.