Click here to Skip to main content
15,892,768 members
Articles / Programming Languages / Visual Basic

Automatic Implementation of the Event-Based Asynchronous Pattern

Rate me:
Please Sign up or sign in to vote.
4.78/5 (32 votes)
26 Nov 2008CPOL20 min read 63.4K   912   101  
Implement the event-based asynchronous pattern automatically with this code generator
'------------------------------------------------------------------------------
' <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 ClientWithMultipleOutputs
        Inherits ClientBase(Of IServerWithMultipleOutputs)

        Private _computeTracker As ComputeTracker

        Public Sub New(ByVal server As IServerWithMultipleOutputs)
            MyBase.New(server)
            Me._computeTracker = New ComputeTracker(Me.server, Me)
            AddHandler Me._computeTracker.OperationCompleted, AddressOf Me._computeTracker_OperationCompleted
            AddHandler Me._computeTracker.ApproximateResultsAvailable, AddressOf Me._computeTracker_ApproximateResultsAvailable
        End Sub

        Public Event ComputeCompleted As AsyncGen.AsyncCompletedEventHandler(Of ComputeOutput)

        Public Event ApproximateResultsAvailable As AsyncGen.ProgressChangedEventHandler(Of IntermediateResults)

        Public Function Compute(ByRef x As Double, ByRef y As Double) As Double
            Dim value As Double
            Me._computeTracker.CreateOperation()
            Try
                value = Me.server.Compute(x, y)
            Finally
                Me._computeTracker.CompleteOperation()
            End Try
            Return value
        End Function

        Public Sub ComputeAsync(ByVal x As Double)
            Me._computeTracker.CreateOperation()
            Dim y As Double
            Dim d As ComputeDelegate = AddressOf Me.server.Compute
            d.BeginInvoke(x, y, AddressOf Me._computeTracker.PostOperationCompleted, Nothing)
        End Sub

        Private Sub _computeTracker_ApproximateResultsAvailable(ByVal sender As Object, ByVal args As AsyncGen.ProgressChangedEventArgs(Of IntermediateResults))
            RaiseEvent ApproximateResultsAvailable(Me, args)
        End Sub

        Private Sub _computeTracker_OperationCompleted(ByVal sender As Object, ByVal args As AsyncGen.AsyncCompletedEventArgs(Of ComputeOutput))
            RaiseEvent ComputeCompleted(Me, args)
        End Sub

        Public Structure ComputeOutput

            Public _ReturnValue As Double

            Public x As Double

            Public y As Double
        End Structure

        Public Structure IntermediateResults

            Public [return] As Double

            Public x As Double

            Public y As Double
        End Structure

        Private Class ComputeTracker
            Inherits OperationTracker(Of IServerWithMultipleOutputs, ClientWithMultipleOutputs, ComputeDelegate, ComputeOutput)
            Implements IServerWithMultipleOutputsCallbacks

            Public Sub New(ByVal server As IServerWithMultipleOutputs, ByVal client As ClientWithMultipleOutputs)
                MyBase.New(server, client)
            End Sub

            Public Event ApproximateResultsAvailable As AsyncGen.ProgressChangedEventHandler(Of IntermediateResults)

            Protected Overrides Sub CallEndInvoke(ByVal d As ComputeDelegate, ByVal iar As System.IAsyncResult, ByRef output As ComputeOutput)
                output.x = CType(Nothing, Double)
                output._ReturnValue = d.EndInvoke(output.x, output.y, iar)
            End Sub

            Protected Overridable Sub OnApproximateResultsAvailable(ByVal args As Object)
                RaiseEvent ApproximateResultsAvailable(Me.client, CType(args, AsyncGen.ProgressChangedEventArgs(Of IntermediateResults)))
            End Sub

            Public Sub ReportApproximateResults(ByVal progress As Integer, ByVal [return] As Double, ByVal x As Double, ByVal y As Double) Implements IServerWithMultipleOutputsCallbacks.ReportApproximateResults
                Dim results As IntermediateResults = New IntermediateResults
                results.[return] = [return]
                results.x = x
                results.y = y
                Me.PostProgress(Of IntermediateResults)(AddressOf Me.OnApproximateResultsAvailable, progress, results)
            End Sub
        End Class

        Delegate Function ComputeDelegate(ByRef x As Double, ByRef y As Double) 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)


Written By
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.

Comments and Discussions