Asynchronous operations

Task-based Asynchronous Pattern (.NET 4.0 or later) 

.NET 4.0 introduced the Task-based Asynchronous Pattern (TAP). Rebex ZIP supports it as well, just reference the assemblies for .NET 4.0 from your project.

// start async operation
Task<ZipItemCollection> t = zip.GetItemsAsync();

// set continuation method
t.ContinueWith(GetItemsFinished);
' start async operation
Dim t As Task(Of ZipItemCollection) = zip.GetItemsAsync()

' set continuation method
t.ContinueWith(AddressOf GetItemsFinished)

Sample asynchronous continuation method:

// this method is called when the async operation is completed
private void GetItemsFinished(Task<ZipItemCollection> t)
{
    // show error if any
    if (t.IsFaulted)
    {
        Console.WriteLine("An error occurred: {0}", t.Exception.ToString());
        return;
    }

    // show cancel notification
    if (t.IsCanceled)
    {
        Console.WriteLine("Operation was canceled.");
        return;
    }

    // get operation result
    ZipItemCollection list = t.Result;

    // show result
    Console.WriteLine("Listed {0} item(s).", list.Count);
}
' this method is called when the async operation is completed
Private Sub GetItemsFinished(t As Task(Of ZipItemCollection))
    ' show error if any
    If t.IsFaulted Then
        Console.WriteLine("An error occurred: {0}", t.Exception.ToString())
        Return
    End If

    ' show cancel notification
    If t.IsCanceled Then
        Console.WriteLine("Operation was canceled.")
        Return
    End If

    ' get operation result
    Dim list As ZipItemCollection = t.Result

    ' show result
    Console.WriteLine("Listed {0} item(s).", list.Count)
End Sub

'await' operator support (.NET 4.5 or later) 

.NET 4.5 introduced the 'await' operator, which makes writing asynchronous code more easy than ever before. Rebex ZIP supports it as well, just reference the assemblies for .NET 4.0 from your project.

private async Task GetItems()
{
    try
    {
        // get list asynchronously
        ZipItemCollection list = await zip.GetItemsAsync();

        // show result
        Console.WriteLine("Listed {0} item(s).", list.Count);
    }
    catch (ZipException ex)
    {
        if (ex.ProblemType == ArchiveProblemType.OperationCanceled)
            Console.WriteLine("Operation was canceled.");
        else
            Console.WriteLine("An error occurred: {0}.", ex.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("An error occurred: {0}", ex.ToString());
    }
}
Private Async Sub GetItems()
    Try
        ' get list asynchronously
        Dim list As ZipItemCollection = Await zip.GetItemsAsync()

        ' show result
        Console.WriteLine("Listed {0} item(s).", list.Count)
    Catch ex As ZipException
        If ex.ProblemType = ArchiveProblemType.OperationCanceled Then
            Console.WriteLine("Operation was canceled.")
        Else
            Console.WriteLine("An error occurred: {0}.", ex.Message)
        End If
    Catch ex As Exception
        Console.WriteLine("An error occurred: {0}", ex.ToString())
    End Try
End Sub

Event-based Asynchronous Pattern 

The Event-based Asynchronous Pattern (EAP), was introduced in .NET 2.0. When you reference Rebex assemblies for .NET 2.0 in your project, this is the pattern you can use.

// register completed event handler
zip.GetItemsCompleted += zip_GetItemsCompleted;

// start async operation
zip.GetItemsAsync();
' register completed event handler
AddHandler zip.GetItemsCompleted, AddressOf GetItemsCompleted

' start async operation
zip.GetItemsAsync()

Sample asynchronous event handler method:

// this method is called when the asynchronous method has completed
private void zip_GetItemsCompleted(object sender, ZipGetItemsCompletedEventArgs e)
{
    // show error if any
    if (e.Error != null)
    {
        Console.WriteLine("An error occurred: {0}", e.Error.ToString());
        return;
    }

    // show cancel notification
    if (e.Cancelled)
    {
        Console.WriteLine("Operation was canceled.");
        return;
    }

    // get operation result
    ZipItemCollection list = e.Result;

    // show result
    Console.WriteLine("Listed {0} item(s).", list.Count);
}
' this method is called when the Async method is completed
Private Sub GetItemsCompleted(sender As Object, e As ZipGetItemsCompletedEventArgs)
    ' show error if any
    If e.Error IsNot Nothing Then
        Console.WriteLine("An error occurred: {0}", e.Error.ToString())
        Return
    End If

    ' show cancel notification
    If e.Cancelled Then
        Console.WriteLine("Operation was canceled.")
        Return
    End If

    ' get operation result
    Dim list As ZipItemCollection = e.Result

    ' show result
    Console.WriteLine("Listed {0} item(s).", list.Count)
End Sub

SynchronizationContext support for events 

When an asynchronous method on the ZipArchive object raises an event, it's dispatched using the synchronization context captured when the operation was started.

This greatly simplifies writing GUI applications (Windows Forms and WPF) because all the events are raised on the applications GUI thread, eliminating the need to explicitly marshal the calls using the Invoke method.

If you need to disable this functionality, set ZipArchive.Options.RaiseEventsFromCurrentThread to true. This will cause all the events to be raised on the background operation's thread.