Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Automatic Implementation of the Event-Based Asynchronous Pattern

, 26 Nov 2008
Implement the event-based asynchronous pattern automatically with this code generator
asyncgen_demo.zip
asyncgen_demo
ClassLibrary1
bin
Debug
ClassDiagram.cd
Properties
Client
bin
Debug
Properties
Settings.settings
Server
bin
Debug
Properties
asyncgen_src.zip
asyncgen_src
AsyncClientGeneratorLib
asyncgen.snk
ClassDiagram.cd
fxReflection.proj
Properties
Properties
TestAssembly
Properties
TestClientCpp
app.ico
TestClientCSharp
Properties
TestClientVB
My Project
Application.myapp
Settings.settings
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:2.0.50727.1433
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On

Imports TestAssembly
Imports AsyncGen
Imports System
Imports System.ComponentModel
Imports System.Diagnostics

Namespace TestAssembly

    Partial Public Class Client
        Inherits ClientBase(Of IServer)

        Private _calculateTracker As CalculateTracker

        Public Sub New(ByVal server As IServer)
            MyBase.New(server)
            Me._calculateTracker = New CalculateTracker(Me.server, Me)
            AddHandler Me._calculateTracker.OperationCompleted, AddressOf Me._calculateTracker_OperationCompleted
            AddHandler Me._calculateTracker.CalculationProgressChanged, AddressOf Me._calculateTracker_CalculationProgressChanged
        End Sub

        Public Event CalculationComplete As AsyncGen.AsyncCompletedEventHandler(Of Double)

        Public Event CalculationProgressChanged As AsyncGen.ProgressChangedEventHandler(Of Double)

        Public Function Calculate(ByVal argument As Double, ByVal userState As Object) As Double
            Dim value As Double
            Me._calculateTracker.CreateOperation(userState)
            Try
                value = Me.server.Calculate(argument, userState, Me._calculateTracker)
            Finally
                Me._calculateTracker.CompleteOperation(userState)
            End Try
            Return value
        End Function

        Public Sub CalculateAsync(ByVal argument As Double, ByVal userState As Object)
            Me._calculateTracker.CreateOperation(userState)
            Dim d As CalculateDelegate = AddressOf Me.server.Calculate
            d.BeginInvoke(argument, userState, Me._calculateTracker, AddressOf Me._calculateTracker.PostOperationCompleted, userState)
        End Sub

        Public Sub CalculateAsyncCancel(ByVal userState As Object)
            If Me._calculateTracker.TryCancelOperation(userState) Then
                Return
            End If
            Throw New System.ArgumentException
        End Sub

        Private Sub _calculateTracker_CalculationProgressChanged(ByVal sender As Object, ByVal args As AsyncGen.ProgressChangedEventArgs(Of Double))
                RaiseEvent CalculationProgressChanged(Me, args)
        End Sub

        Private Sub _calculateTracker_OperationCompleted(ByVal sender As Object, ByVal args As AsyncGen.AsyncCompletedEventArgs(Of Double))
                RaiseEvent CalculationComplete(Me, args)
        End Sub

        Private Class CalculateTracker
            Inherits OperationTracker(Of IServer, Client, CalculateDelegate, Double)
            Implements IServerCallbacks

            Public Sub New(ByVal server As IServer, ByVal client As Client)
                MyBase.New(server, client)
            End Sub

            Public Event CalculationProgressChanged As AsyncGen.ProgressChangedEventHandler(Of Double)

            Protected Overrides Sub CallEndInvoke(ByVal d As CalculateDelegate, ByVal iar As System.IAsyncResult, ByRef output As Double)
                output = d.EndInvoke(iar)
            End Sub

            Protected Overridable Sub OnCalculationProgressChanged(ByVal args As Object)
                RaiseEvent CalculationProgressChanged(Me.client, CType(args,AsyncGen.ProgressChangedEventArgs(Of Double)))
            End Sub

            Public Function ReportCalculationProgress(ByVal approximateResult As Double, ByVal percentage As Integer, ByVal userState As Object) As Boolean Implements IServerCallbacks.ReportCalculationProgress
                Me.PostProgress(Of Double)(AddressOf Me.OnCalculationProgressChanged, percentage, approximateResult, userState)
                Return Me.IsOperationCancelled(userState)
            End Function
        End Class

        Delegate Function CalculateDelegate(ByVal argument As Double, ByVal userState As Object, ByVal callbacks As IServerCallbacks) As Double
    End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ron A Inbar
Software Developer (Senior) Philips Healthcare
Israel Israel
I got my B.Sc. in Mathematics and Computer Science from Tel Aviv University in 1997. Since then I have developed software in UNIX, Win32 and .NET. I currently live in Haifa.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 26 Nov 2008
Article Copyright 2008 by Ron A Inbar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid