Export (0) Print
Expand All

Walkthrough: Implementing a Component That Supports the Event-based Asynchronous Pattern

If you are writing a class with some operations that may incur noticeable delays, consider giving it asynchronous functionality by implementing the Event-based Asynchronous Pattern Overview.

This walkthrough illustrates how to create a component that implements the Event-based Asynchronous Pattern. It is implemented using helper classes from the System.ComponentModel namespace, which ensures that the component works correctly under any application model, including ASP.NET, Console applications and Windows Forms applications. This component is also designable with a PropertyGrid control and your own custom designers.

When you are through, you will have an application that computes prime numbers asynchronously. Your application will have a main user interface (UI) thread and a thread for each prime number calculation. Although testing whether a large number is prime can take a noticeable amount of time, the main UI thread will not be interrupted by this delay, and the form will be responsive during the calculations. You will be able to run as many calculations as you like concurrently and selectively cancel pending calculations.

Tasks illustrated in this walkthrough include:

  • Creating the Component

  • Defining Public Asynchronous Events and Delegates

  • Defining Private Delegates

  • Implementing Public Events

  • Implementing the Completion Method

  • Implementing the Worker Methods

  • Implementing Start and Cancel Methods

To copy the code in this topic as a single listing, see How to: Implement a Component That Supports the Event-based Asynchronous Pattern.

The first step is to create the component that will implement the Event-based Asynchronous Pattern.

To create the component

  • Create a class called PrimeNumberCalculator that inherits from Component.

Your component communicates to clients using events. The MethodNameCompleted event alerts clients to the completion of an asynchronous task, and the MethodNameProgressChanged event informs clients of the progress of an asynchronous task.

To define asynchronous events for clients of your component:

  1. Import the System.Threading and System.Collections.Specialized namespaces at the top of your file.

    Imports System
    Imports System.Collections
    Imports System.Collections.Specialized
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Globalization
    Imports System.Threading
    Imports System.Windows.Forms
    
  2. Before the PrimeNumberCalculator class definition, declare delegates for progress and completion events.

    Public Delegate Sub ProgressChangedEventHandler( _
        ByVal e As ProgressChangedEventArgs)
    
    Public Delegate Sub CalculatePrimeCompletedEventHandler( _
        ByVal sender As Object, _
        ByVal e As CalculatePrimeCompletedEventArgs)
    
  3. In the PrimeNumberCalculator class definition, declare events for reporting progress and completion to clients.

    Public Event ProgressChanged _
        As ProgressChangedEventHandler
    Public Event CalculatePrimeCompleted _
        As CalculatePrimeCompletedEventHandler
    
  4. After the PrimeNumberCalculator class definition, derive the CalculatePrimeCompletedEventArgs class for reporting the outcome of each calculation to the client's event handler for the CalculatePrimeCompleted.event. In addition to the AsyncCompletedEventArgs properties, this class enables the client to determine what number was tested, whether it is prime, and what the first divisor is if it is not prime.

    Public Class CalculatePrimeCompletedEventArgs
        Inherits AsyncCompletedEventArgs
        Private numberToTestValue As Integer = 0
        Private firstDivisorValue As Integer = 1
        Private isPrimeValue As Boolean 
    
    
        Public Sub New( _
        ByVal numberToTest As Integer, _
        ByVal firstDivisor As Integer, _
        ByVal isPrime As Boolean, _
        ByVal e As Exception, _
        ByVal canceled As Boolean, _
        ByVal state As Object)
    
            MyBase.New(e, canceled, state)
            Me.numberToTestValue = numberToTest
            Me.firstDivisorValue = firstDivisor
            Me.isPrimeValue = isPrime
    
        End Sub 
    
    
        Public ReadOnly Property NumberToTest() As Integer 
            Get 
                ' Raise an exception if the operation failed  
                ' or was canceled.
                RaiseExceptionIfNecessary()
    
                ' If the operation was successful, return  
                ' the property value. 
                Return numberToTestValue
            End Get 
        End Property 
    
    
        Public ReadOnly Property FirstDivisor() As Integer 
            Get 
                ' Raise an exception if the operation failed  
                ' or was canceled.
                RaiseExceptionIfNecessary()
    
                ' If the operation was successful, return  
                ' the property value. 
                Return firstDivisorValue
            End Get 
        End Property 
    
    
        Public ReadOnly Property IsPrime() As Boolean 
            Get 
                ' Raise an exception if the operation failed  
                ' or was canceled.
                RaiseExceptionIfNecessary()
    
                ' If the operation was successful, return  
                ' the property value. 
                Return isPrimeValue
            End Get 
        End Property 
    End Class
    

At this point, you can build the component.

To test your component

  • Compile the component.

    You will receive two compiler warnings:

    warning CS0067: The event 'AsynchronousPatternExample.PrimeNumberCalculator.ProgressChanged' is never used
    warning CS0067: The event 'AsynchronousPatternExample.PrimeNumberCalculator.CalculatePrimeCompleted' is never used
    

    These warnings will be cleared in the next section.

The asynchronous aspects of the PrimeNumberCalculator component are implemented internally with a special delegate known as a SendOrPostCallback. A SendOrPostCallback represents a callback method that executes on a ThreadPool thread. The callback method must have a signature that takes a single parameter of type Object, which means you will need to pass state among delegates in a wrapper class. For more information, see SendOrPostCallback.

To implement your component's internal asynchronous behavior:

  1. Declare and create the SendOrPostCallback delegates in the PrimeNumberCalculator class. Create the SendOrPostCallback objects in a utility method called InitializeDelegates.

    You will need two delegates: one for reporting progress to the client, and one for reporting completion to the client.

    Private onProgressReportDelegate As SendOrPostCallback
    Private onCompletedDelegate As SendOrPostCallback
    
    
    ...
    
    
    Protected Overridable Sub InitializeDelegates()
        onProgressReportDelegate = _
            New SendOrPostCallback(AddressOf ReportProgress)
        onCompletedDelegate = _
            New SendOrPostCallback(AddressOf CalculateCompleted)
    End Sub
    
  2. Call the InitializeDelegates method in your component's constructor.

    Public Sub New()
    
        InitializeComponent()
    
        InitializeDelegates()
    
    End Sub
    
  3. Declare a delegate in the PrimeNumberCalculator class that handles the actual work to be done asynchronously. This delegate wraps the worker method that tests whether a number is prime. The delegate takes an AsyncOperation parameter, which will be used to track the lifetime of the asynchronous operation.

    Private Delegate Sub WorkerEventHandler( _
    ByVal numberToCheck As Integer, _
    ByVal asyncOp As AsyncOperation)
    
  4. Create a collection for managing lifetimes of pending asynchronous operations. The client needs a way to track operations as they are executed and completed, and this tracking is done by requiring the client to pass a unique token, or task ID, when the client makes the call to the asynchronous method. The PrimeNumberCalculator component must keep track of each call by associating the task ID with its corresponding invocation. If the client passes a task ID that is not unique, the PrimeNumberCalculator component must raise an exception.

    The PrimeNumberCalculator component keeps track of task ID by using a special collection class called a HybridDictionary. In the class definition, create a HybridDictionary called userTokenToLifetime.

    Private userStateToLifetime As New HybridDictionary()
    

Components that implement the Event-based Asynchronous Pattern communicate to clients using events. These events are invoked on the proper thread with the help of the AsyncOperation class.

To raise events to your component's clients:

  • Implement public events for reporting to clients. You will need an event for reporting progress and one for reporting completion.

    ' This method is invoked via the AsyncOperation object, 
    ' so it is guaranteed to be executed on the correct thread. 
    Private Sub CalculateCompleted(ByVal operationState As Object)
        Dim e As CalculatePrimeCompletedEventArgs = operationState
    
        OnCalculatePrimeCompleted(e)
    
    End Sub 
    
    
    ' This method is invoked via the AsyncOperation object, 
    ' so it is guaranteed to be executed on the correct thread. 
    Private Sub ReportProgress(ByVal state As Object)
        Dim e As ProgressChangedEventArgs = state
    
        OnProgressChanged(e)
    
    End Sub 
    
    Protected Sub OnCalculatePrimeCompleted( _
        ByVal e As CalculatePrimeCompletedEventArgs)
    
        RaiseEvent CalculatePrimeCompleted(Me, e)
    
    End Sub 
    
    
    Protected Sub OnProgressChanged( _
        ByVal e As ProgressChangedEventArgs)
    
        RaiseEvent ProgressChanged(e)
    
    End Sub
    

The completion delegate is the method that the underlying, free-threaded asynchronous behavior will invoke when the asynchronous operation ends by successful completion, error, or cancellation. This invocation happens on an arbitrary thread.

This method is where the client's task ID is removed from the internal collection of unique client tokens. This method also ends the lifetime of a particular asynchronous operation by calling the PostOperationCompleted method on the corresponding AsyncOperation. This call raises the completion event on the thread that is appropriate for the application model. After the PostOperationCompleted method is called, this instance of AsyncOperation can no longer be used, and any subsequent attempts to use it will throw an exception.

The CompletionMethod signature must hold all state necessary to describe the outcome of the asynchronous operation. It holds state for the number that was tested by this particular asynchronous operation, whether the number is prime, and the value of its first divisor if it is not a prime number. It also holds state describing any exception that occurred, and the AsyncOperation corresponding to this particular task.

To complete an asynchronous operation:

  • Implement the completion method. It takes six parameters, which it uses to populate a CalculatePrimeCompletedEventArgs that is returned to the client through the client's CalculatePrimeCompletedEventHandler. It removes the client's task ID token from the internal collection, and it ends the asynchronous operation's lifetime with a call to PostOperationCompleted. The AsyncOperation marshals the call to the thread or context that is appropriate for the application model.

    ' This is the method that the underlying, free-threaded  
    ' asynchronous behavior will invoke.  This will happen on 
    '  an arbitrary thread. 
    Private Sub CompletionMethod( _
        ByVal numberToTest As Integer, _
        ByVal firstDivisor As Integer, _
        ByVal prime As Boolean, _
        ByVal exc As Exception, _
        ByVal canceled As Boolean, _
        ByVal asyncOp As AsyncOperation)
    
        ' If the task was not previously canceled, 
        ' remove the task from the lifetime collection. 
        If Not canceled Then 
            SyncLock userStateToLifetime.SyncRoot
                userStateToLifetime.Remove(asyncOp.UserSuppliedState)
            End SyncLock 
        End If 
    
        ' Package the results of the operation in a  
        ' CalculatePrimeCompletedEventArgs. 
        Dim e As New CalculatePrimeCompletedEventArgs( _
            numberToTest, _
            firstDivisor, _
            prime, _
            exc, _
            canceled, _
            asyncOp.UserSuppliedState)
    
        ' End the task. The asyncOp object is responsible  
        ' for marshaling the call.
        asyncOp.PostOperationCompleted(onCompletedDelegate, e)
    
        ' Note that after the call to PostOperationCompleted, asyncOp 
        ' is no longer usable, and any attempt to use it will cause. 
        ' an exception to be thrown. 
    
    End Sub
    

At this point, you can build the component.

To test your component

  • Compile the component.

    You will receive one compiler warning:

    warning CS0169: The private field 'AsynchronousPatternExample.PrimeNumberCalculator.workerDelegate' is never used
    

    This warning will be resolved in the next section.

So far, you have implemented the supporting asynchronous code for the PrimeNumberCalculator component. Now you can implement the code that does the actual work. You will implement three methods: CalculateWorker, BuildPrimeNumberList, and IsPrime. Together, BuildPrimeNumberList and IsPrime comprise a well-known algorithm called the Sieve of Eratosthenes, which determines if a number is prime by finding all the prime numbers up to the square root of the test number. If no divisors are found by that point, the test number is prime.

If this component were written for maximum efficiency, it would remember all the prime numbers discovered by various invocations for different test numbers. It would also check for trivial divisors like 2, 3, and 5. The intent of this example is to demonstrate how time-consuming operations can be executed asynchronously, however, so these optimizations are left as an exercise for you.

The CalculateWorker method is wrapped in a delegate and is invoked asynchronously with a call to BeginInvoke.

Note Note

Progress reporting is implemented in the BuildPrimeNumberList method. On fast computers, ProgressChanged events can be raised in rapid succession. The client thread, on which these events are raised, must be able to handle this situation. User interface code may be flooded with messages and unable to keep up, resulting in hanging behavior. For an example user interface that handles this situation, see How to: Implement a Client of the Event-based Asynchronous Pattern.

To execute the prime number calculation asynchronously:

  1. Implement the TaskCanceled utility method. This checks the task lifetime collection for the given task ID, and returns true if the task ID is not found.

    ' Utility method for determining if a  
    ' task has been canceled. 
    Private Function TaskCanceled(ByVal taskId As Object) As Boolean 
        Return (userStateToLifetime(taskId) Is Nothing)
    End Function
    
  2. Implement the CalculateWorker method. It takes two parameters: a number to test, and an AsyncOperation.

    ' This method performs the actual prime number computation. 
    ' It is executed on the worker thread. 
    Private Sub CalculateWorker( _
        ByVal numberToTest As Integer, _
        ByVal asyncOp As AsyncOperation)
    
        Dim prime As Boolean = False 
        Dim firstDivisor As Integer = 1
        Dim exc As Exception = Nothing 
    
        ' Check that the task is still active. 
        ' The operation may have been canceled before 
        ' the thread was scheduled. 
        If Not Me.TaskCanceled(asyncOp.UserSuppliedState) Then 
    
            Try 
                ' Find all the prime numbers up to the 
                ' square root of numberToTest. 
                Dim primes As ArrayList = BuildPrimeNumberList( _
                    numberToTest, asyncOp)
    
                ' Now we have a list of primes less than  
                'numberToTest.
                prime = IsPrime( _
                    primes, _
                    numberToTest, _
                    firstDivisor)
    
            Catch ex As Exception
                exc = ex
            End Try 
    
        End If 
    
        Me.CompletionMethod( _
            numberToTest, _
            firstDivisor, _
            prime, _
            exc, _
            TaskCanceled(asyncOp.UserSuppliedState), _
            asyncOp)
    
    End Sub
    
  3. Implement BuildPrimeNumberList. It takes two parameters: the number to test, and an AsyncOperation. It uses the AsyncOperation to report progress and incremental results. This assures that the client's event handlers are called on the proper thread or context for the application model. When BuildPrimeNumberList finds a prime number, it reports this as an incremental result to the client's event handler for the ProgressChanged event. This requires a class derived from ProgressChangedEventArgs, called CalculatePrimeProgressChangedEventArgs, which has one added property called LatestPrimeNumber.

    The BuildPrimeNumberList method also periodically calls the TaskCanceled method and exits if the method returns true.

    ' This method computes the list of prime numbers used by the 
    ' IsPrime method. 
    Private Function BuildPrimeNumberList( _
        ByVal numberToTest As Integer, _
        ByVal asyncOp As AsyncOperation) As ArrayList
    
        Dim e As ProgressChangedEventArgs = Nothing 
        Dim primes As New ArrayList
        Dim firstDivisor As Integer 
        Dim n As Integer = 5
    
        ' Add the first prime numbers.
        primes.Add(2)
        primes.Add(3)
    
        ' Do the work. 
        While n < numberToTest And _
            Not Me.TaskCanceled(asyncOp.UserSuppliedState)
    
            If IsPrime(primes, n, firstDivisor) Then 
                ' Report to the client that you found a prime.
                e = New CalculatePrimeProgressChangedEventArgs( _
                    n, _
                    CSng(n) / CSng(numberToTest) * 100, _
                    asyncOp.UserSuppliedState)
    
                asyncOp.Post(Me.onProgressReportDelegate, e)
    
                primes.Add(n)
    
                ' Yield the rest of this time slice.
                Thread.Sleep(0)
            End If 
    
            ' Skip even numbers.
            n += 2
    
        End While 
    
        Return primes
    
    End Function
    
  4. Implement IsPrime. It takes three parameters: a list of known prime numbers, the number to test, and an output parameter for the first divisor found. Given the list of prime numbers, it determines if the test number is prime.

    ' This method tests n for primality against the list of  
    ' prime numbers contained in the primes parameter. 
    Private Function IsPrime( _
        ByVal primes As ArrayList, _
        ByVal n As Integer, _
        ByRef firstDivisor As Integer) As Boolean 
    
        Dim foundDivisor As Boolean = False 
        Dim exceedsSquareRoot As Boolean = False 
    
        Dim i As Integer = 0
        Dim divisor As Integer = 0
        firstDivisor = 1
    
        ' Stop the search if: 
        ' there are no more primes in the list, 
        ' there is a divisor of n in the list, or 
        ' there is a prime that is larger than  
        ' the square root of n. 
        While i < primes.Count AndAlso _
            Not foundDivisor AndAlso _
            Not exceedsSquareRoot
    
            ' The divisor variable will be the smallest prime number  
            ' not yet tried.
            divisor = primes(i)
            i = i + 1
    
            ' Determine whether the divisor is greater than the  
            ' square root of n. 
            If divisor * divisor > n Then
                exceedsSquareRoot = True 
                ' Determine whether the divisor is a factor of n. 
            ElseIf n Mod divisor = 0 Then
                firstDivisor = divisor
                foundDivisor = True 
            End If 
        End While 
    
        Return Not foundDivisor
    
    End Function
    
  5. Derive CalculatePrimeProgressChangedEventArgs from ProgressChangedEventArgs. This class is necessary for reporting incremental results to the client's event handler for the ProgressChanged event. It has one added property called LatestPrimeNumber.

    Public Class CalculatePrimeProgressChangedEventArgs
        Inherits ProgressChangedEventArgs
        Private latestPrimeNumberValue As Integer = 1
    
    
        Public Sub New( _
            ByVal latestPrime As Integer, _
            ByVal progressPercentage As Integer, _
            ByVal UserState As Object)
    
            MyBase.New(progressPercentage, UserState)
            Me.latestPrimeNumberValue = latestPrime
    
        End Sub 
    
        Public ReadOnly Property LatestPrimeNumber() As Integer 
            Get 
                Return latestPrimeNumberValue
            End Get 
        End Property 
    End Class
    

At this point, you can build the component.

To test your component

  • Compile the component.

    All that remains to be written are the methods to start and cancel asynchronous operations, CalculatePrimeAsync and CancelAsync.

You start the worker method on its own thread by calling BeginInvoke on the delegate that wraps it. To manage the lifetime of a particular asynchronous operation, you call the CreateOperation method on the AsyncOperationManager helper class. This returns an AsyncOperation, which marshals calls on the client's event handlers to the proper thread or context.

You cancel a particular pending operation by calling PostOperationCompleted on its corresponding AsyncOperation. This ends that operation, and any subsequent calls to its AsyncOperation will throw an exception.

To implement Start and Cancel functionality:

  1. Implement the CalculatePrimeAsync method. Make sure the client-supplied token (task ID) is unique with respect to all the tokens representing currently pending tasks. If the client passes in a non-unique token, CalculatePrimeAsync raises an exception. Otherwise, the token is added to the task ID collection.

    ' This method starts an asynchronous calculation.  
    ' First, it checks the supplied task ID for uniqueness. 
    ' If taskId is unique, it creates a new WorkerEventHandler  
    ' and calls its BeginInvoke method to start the calculation. 
    Public Overridable Sub CalculatePrimeAsync( _
        ByVal numberToTest As Integer, _
        ByVal taskId As Object)
    
        ' Create an AsyncOperation for taskId. 
        Dim asyncOp As AsyncOperation = _
            AsyncOperationManager.CreateOperation(taskId)
    
        ' Multiple threads will access the task dictionary, 
        ' so it must be locked to serialize access. 
        SyncLock userStateToLifetime.SyncRoot
            If userStateToLifetime.Contains(taskId) Then 
                Throw New ArgumentException( _
                    "Task ID parameter must be unique", _
                    "taskId")
            End If
    
            userStateToLifetime(taskId) = asyncOp
        End SyncLock 
    
        ' Start the asynchronous operation. 
        Dim workerDelegate As New WorkerEventHandler( _
            AddressOf CalculateWorker)
    
        workerDelegate.BeginInvoke( _
            numberToTest, _
            asyncOp, _
            Nothing, _
            Nothing)
    
    End Sub
    
  2. Implement the CancelAsync method. If the taskId parameter exists in the token collection, it is removed. This prevents canceled tasks that have not started from running. If the task is running, the BuildPrimeNumberList method exits when it detects that the task ID has been removed from the lifetime collection.

    ' This method cancels a pending asynchronous operation. 
    Public Sub CancelAsync(ByVal taskId As Object)
    
        Dim obj As Object = userStateToLifetime(taskId)
        If (obj IsNot Nothing) Then 
    
            SyncLock userStateToLifetime.SyncRoot
    
                userStateToLifetime.Remove(taskId)
    
            End SyncLock 
    
        End If 
    
    End Sub
    

At this point, you can build the component.

To test your component

  • Compile the component.

The PrimeNumberCalculator component is now complete and ready to use.

For an example client that uses the PrimeNumberCalculator component, see How to: Implement a Client of the Event-based Asynchronous Pattern.

You can fill out this example by writing CalculatePrime, the synchronous equivalent of CalculatePrimeAsync method. This will make the PrimeNumberCalculator component fully compliant with the Event-based Asynchronous Pattern.

You can improve this example by retaining the list of all the prime numbers discovered by various invocations for different test numbers. Using this approach, each task will benefit from the work done by previous tasks. Be careful to protect this list with lock regions, so access to the list by different threads is serialized.

You can also improve this example by testing for trivial divisors, like 2, 3, and 5.

Show:
© 2014 Microsoft