Click here to Skip to main content
11,718,289 members (84,215 online)
Click here to Skip to main content
Add your own
alternative version

Automatic Implementation of the Event-Based Asynchronous Pattern

, 26 Nov 2008 CPOL 40.4K 698 99
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 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)

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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150901.1 | Last Updated 26 Nov 2008
Article Copyright 2008 by Ron A Inbar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid