Click here to Skip to main content
15,886,873 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.2K   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 OverloadedClient
        Inherits ClientBase(Of IOverloadedServer)

        Private _op1Tracker As op1Tracker

        Private _op2Tracker As op2Tracker

        Public Sub New(ByVal server As IOverloadedServer)
            MyBase.New(server)
            Me._op1Tracker = New op1Tracker(Me.server, Me)
            AddHandler Me._op1Tracker.OperationCompleted, AddressOf Me._op1Tracker_OperationCompleted
            Me._op2Tracker = New op2Tracker(Me.server, Me)
            AddHandler Me._op2Tracker.OperationCompleted, AddressOf Me._op2Tracker_OperationCompleted
        End Sub

        Public Event DoSomethingCompleted As System.ComponentModel.AsyncCompletedEventHandler

        Public Overloads Sub DoSomething(ByVal m As Integer)
            Me._op1Tracker.CreateOperation
            Try
                Me.server.DoSomething(m)
            Finally
                Me._op1Tracker.CompleteOperation
            End Try
        End Sub

        Public Overloads Sub DoSomethingAsync(ByVal m As Integer)
            Me._op1Tracker.CreateOperation
            Dim d As op1Delegate = AddressOf Me.server.DoSomething
            d.BeginInvoke(m, AddressOf Me._op1Tracker.PostOperationCompleted, Nothing)
        End Sub

        Public Sub DoSomethingAsyncCancel()
            If Me._op1Tracker.TryCancelOperation Then
                Return
            End If
            If Me._op2Tracker.TryCancelOperation Then
                Return
            End If
            Throw New System.ArgumentException
        End Sub

        Private Sub _op1Tracker_OperationCompleted(ByVal sender As Object, ByVal args As System.ComponentModel.AsyncCompletedEventArgs)
                RaiseEvent DoSomethingCompleted(Me, args)
        End Sub

        Public Overloads Sub DoSomething(ByVal m As Integer, ByVal n As Integer)
            Me._op2Tracker.CreateOperation
            Try
                Me.server.DoSomething(m, n)
            Finally
                Me._op2Tracker.CompleteOperation
            End Try
        End Sub

        Public Overloads Sub DoSomethingAsync(ByVal m As Integer, ByVal n As Integer)
            Me._op2Tracker.CreateOperation
            Dim d As op2Delegate = AddressOf Me.server.DoSomething
            d.BeginInvoke(m, n, AddressOf Me._op2Tracker.PostOperationCompleted, Nothing)
        End Sub

        Private Sub _op2Tracker_OperationCompleted(ByVal sender As Object, ByVal args As System.ComponentModel.AsyncCompletedEventArgs)
                RaiseEvent DoSomethingCompleted(Me, args)
        End Sub

        Private Class op1Tracker
            Inherits OperationTracker(Of IOverloadedServer, OverloadedClient, op1Delegate)

            Public Sub New(ByVal server As IOverloadedServer, ByVal client As OverloadedClient)
                MyBase.New(server, client)
            End Sub

            Protected Overrides Sub CallEndInvoke(ByVal d As op1Delegate, ByVal iar As System.IAsyncResult)
                d.EndInvoke(iar)
            End Sub
        End Class

        Delegate Sub op1Delegate(ByVal m As Integer)

        Private Class op2Tracker
            Inherits OperationTracker(Of IOverloadedServer, OverloadedClient, op2Delegate)

            Public Sub New(ByVal server As IOverloadedServer, ByVal client As OverloadedClient)
                MyBase.New(server, client)
            End Sub

            Protected Overrides Sub CallEndInvoke(ByVal d As op2Delegate, ByVal iar As System.IAsyncResult)
                d.EndInvoke(iar)
            End Sub
        End Class

        Delegate Sub op2Delegate(ByVal m As Integer, ByVal n As Integer)
    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