VB.NET TaskCompleted Event for Asynchronous Method Call

Demonstrates a TaskCompleted event for an asynchronous Chilkat method call.
The event callback occurs in the background thread, and therefore any updates to the UI must
happen on the UI thread. For this reason, MethodInvoker is used to make updates to a TextBox..

    Dim WithEvents http As New Chilkat.Http()

    Private Sub http_OnTaskCompleted(sender As Object, args As Chilkat.TaskCompletedEventArgs) Handles http.OnTaskCompleted

        Dim task As Chilkat.Task = args.Task

        ' This event callback is running in the background thread.
        ' To update a UI element, we must be on the UI thread..
        Me.Invoke(New MethodInvoker(
               ' The task.UserData can be used to identify the particular asynchronous method call
               ' that this callback belongs to.
               If (task.UserData = "chilkatHomePage") Then
                   ' An asychronous method is simply calling the corresponding synchronous method
                   ' in a background thread.  In this case, it is a call to QuickGetStr,
                   ' which returned a string, or it returned Nothing for failure.  This result is available
                   ' via task.GetResultString.  The LastErrorText for the background call 
                   ' is available in task.ResultErrorText
                   Dim htmlPage As String = task.GetResultString()
                   If (htmlPage Is Nothing) Then
                       TextBox1.Text = task.ResultErrorText
                       TextBox1.Text = htmlPage
                   End If
               End If
           End Sub))
    End Sub

    Private Sub AsyncToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AsyncToolStripMenuItem.Click

        http = New Chilkat.Http()

        Dim task As Chilkat.Task = http.QuickGetStrAsync("http://www.chilkatsoft.com/")
        If (task Is Nothing) Then
            TextBox1.Text = http.LastErrorText
            Exit Sub
        End If

        ' We can set the task.UserData property to identify this particular asynchronous call in the callback.
        task.UserData = "chilkatHomePage"

        ' Start the task in a background thread
        Dim success As Boolean = task.Run()
        If (Not success) Then
            TextBox1.Text = task.LastErrorText
            Exit Sub
        End If

    End Sub

Adding a Callback Event Handler in VB.NET

  1. Dim “WithEvents”
    Dim WithEvents ftp As Chilkat.Ftp2
  2. Add some code to create a new instance of the object.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ftp = New Chilkat.Ftp2()
            ftp.EnableEvents = True
            ftp.HeartbeatMs = 100
  3. Select the “ftp” object.

  4. Select the event. This causes Visual Studio to generate the event handler function.

  5. Add your application code to the event handler function.
    Private Sub ftp_OnAbortCheck(ByVal sender As Object, ByVal args As Chilkat.AbortCheckEventArgs) Handles ftp.OnAbortCheck
        ' Add your application code here...
        End Sub

Is Calling .Dispose() Recommended for C# and VB.NET?


In your examples online, we have noticed that .Dispose() is never
called after using Chilkat classes (we are writing in C#).

Is calling .Dispose() recommended?  Does your code not need to destroy
any native resources or handles that would normally be cleaned up in
the Dispose method (the IDisposeable interface) ?


For objects that manage a TCP/IP socket connection with a server (FTP2, SSH, SFTP, POP3, SMTP, IMAP, HTTP, etc.) then calling Dispose will close the connection.  Aside from memory-usage and socket connections, there are no other resources used by Chilkat classes.  Dispose may also deallocate some internal memory.

The important thing to realize is that once Dispose is called, your code should not use that particular instance of the object again.  I always prefer a more explicit approach.  For example, instead of calling Chilkat.MailMan.Dispose to ensure that a connection to the SMTP server is closed, call Chilkat.MailMan.CloseSmtpConnection.  (If the mailman is not actually connected, calling CloseSmtpConnection does NOT result in an error.  It is harmless.)

Also, when an Chilkat object’s finalizer is called (i.e. when it is garbage-collected), any existing socket connections are closed and internal memory is (of course) deallocated.   You may force garbage collection by doing this (in C#)


VB.NET HTTP Download with percent-done progress monitoring

Here is an example:

    Dim WithEvents http As Chilkat.Http

    Private Sub http_OnPercentDone(ByVal sender As Object, ByVal args As Chilkat.PercentDoneEventArgs) Handles http.OnPercentDone
        ProgressBar1.Value = args.PercentDone
    End Sub

    Private Sub HttpDownloadTest()

        http = New Chilkat.Http()

        Dim success As Boolean

        '  Any string unlocks the component for the 1st 30-days.
        success = http.UnlockComponent("Anything for 30-day trial")
        If (success <> True) Then
            Exit Sub
        End If

        ' Enable event callbacks...
        http.EnableEvents = True

        '  Download the Python language install.
        '  Note: This URL may have changed since this example was created.
        success = http.Download("http://www.python.org/ftp/python/2.5/python-2.5.msi", "python-2.5.msi")
        If (success <> True) Then
            MsgBox("Python Download Complete!")
        End If

        http = Nothing

    End Sub

VB.NET Compress String to Byte Array

Demonstrates how to use Chilkat.Compression to compress a string to a byte array:

    Private Sub CompressStringToBytes()

        Dim compress As New Chilkat.Compression()

        '  Any string argument automatically begins a 30-day trial.
        Dim success As Boolean
        success = compress.UnlockComponent("30-day trial")
        If (success <> True) Then
            MsgBox("Compression component unlock failed")
            Exit Sub
        End If

        ' Use the "deflate" algorithm, which is the algorithm used
        ' for .zip.  Other algorithms are "lzw", "bzip2", and "ppmd"
        compress.Algorithm = "deflate"

        Dim strData As String

        '  Create a string to compress:
        strData = "hellohellohellohellohellohellohellohellohellohello"
        MessageBox.Show("Uncompressed size: " & strData.Length.ToString())

        '  Tell the component to convert to
        '  ansi (1-byte/char) prior to compression:
        compress.Charset = "ansi"

        Dim compressedData As Byte()
        compressedData = compress.CompressString(strData)
        MessageBox.Show("Compressed size: " & compressedData.Length.ToString())

        ' Decompress:
        Dim origStr As String
        origStr = compress.DecompressString(compressedData)

    End Sub