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(
           Sub()
               ' 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
                   Else
                       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

Chilkat Success/Failure Return Status Values in VB.NET vs. VB6

When programming in VB.NET or VB6, be careful that you are looking at the correct online examples.

VB6 examples are located here.

whereas

VB.NET examples are located here.

In VB6, it is the Chilkat ActiveX that is used, and return values (in general) are 1 for success and 0 for failure. However, in VB.NET, return values are True for success and False for failure. The value True is not equal to 1. Therefore, if programming in VB.NET, do not check for 1/0 return values, make sure to check for True/False.

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?

Question:

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) ?

Answer:

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#)

 GC.Collect();
 GC.WaitForPendingFinalizers();

VB.NET to Extract a File from one Zip Archive and Append it to Another Zip Archive

The following VB.NET code copies the contents of the nutrition.xml file (found in xml1.zip) to another pre-existing .zip named xml2.zip:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim zip1 As New Chilkat.Zip
        Dim success As Boolean

        success = zip1.UnlockComponent("Anything for 30-day trial")

        success = zip1.OpenZip("xml1.zip")

        Dim entry As Chilkat.ZipEntry
        entry = zip1.GetEntryByName("nutrition.xml")
        ' Assume the entry was found and is non-null

        ' Now we'll add this entry to a different already-existing zip archive.
        ' To avoid re-writing the entire zip, we use QuickAppend.
        ' In this example, the already-existing .zip is "xml2.zip"

        ' Create a new zip object for the purpose of holding the entries that will
        ' be appended to xml2.zip
        Dim zipTemp As New Chilkat.Zip
        zipTemp.NewZip("thisIsADummyZipObjectAndIsNeverWritten.zip")

        ' Retrieve the compressed data from the entry object and add it to our
        ' temporary zip object.
        zipTemp.AppendCompressed("nutrition.xml", entry.Copy())

        ' Append all the entries in zipTemp to the existing zip file named "xml2.zip"
        ' This writes the entries in zipTemp (all in memory) to xml2.zip
        success = zipTemp.QuickAppend("xml2.zip")


    End Sub

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
            MsgBox(http.LastErrorText)
            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(http.LastErrorText)
        Else
            MsgBox("Python Download Complete!")
        End If

        http = Nothing

    End Sub