Click here to Skip to main content
15,884,298 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>
//------------------------------------------------------------------------------

namespace TestAssembly
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using AsyncGen;


    public partial class OverloadedClient : ClientBase<TestAssembly.IOverloadedServer>
    {

        private op1Tracker _op1Tracker;

        private op2Tracker _op2Tracker;

        public OverloadedClient(TestAssembly.IOverloadedServer server) : 
                base(server)
        {
            this._op1Tracker = new op1Tracker(this.server, this);
            this._op1Tracker.OperationCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this._op1Tracker_OperationCompleted);
            this._op2Tracker = new op2Tracker(this.server, this);
            this._op2Tracker.OperationCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this._op2Tracker_OperationCompleted);
        }

        public event System.ComponentModel.AsyncCompletedEventHandler DoSomethingCompleted;

        public void DoSomething(int m)
        {
            this._op1Tracker.CreateOperation();
            try
            {
                this.server.DoSomething(m);
            }
            finally
            {
                this._op1Tracker.CompleteOperation();
            }
        }

        public void DoSomethingAsync(int m)
        {
            this._op1Tracker.CreateOperation();
            op1Delegate d = new op1Delegate(this.server.DoSomething);
            d.BeginInvoke(m, new System.AsyncCallback(this._op1Tracker.PostOperationCompleted), null);
        }

        public void DoSomethingAsyncCancel()
        {
            if (this._op1Tracker.TryCancelOperation())
            {
                return;
            }
            if (this._op2Tracker.TryCancelOperation())
            {
                return;
            }
            throw new System.ArgumentException();
        }

        private void _op1Tracker_OperationCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs args)
        {
            if ((this.DoSomethingCompleted != null))
            {
                this.DoSomethingCompleted(this, args);
            }
        }

        public void DoSomething(int m, int n)
        {
            this._op2Tracker.CreateOperation();
            try
            {
                this.server.DoSomething(m, n);
            }
            finally
            {
                this._op2Tracker.CompleteOperation();
            }
        }

        public void DoSomethingAsync(int m, int n)
        {
            this._op2Tracker.CreateOperation();
            op2Delegate d = new op2Delegate(this.server.DoSomething);
            d.BeginInvoke(m, n, new System.AsyncCallback(this._op2Tracker.PostOperationCompleted), null);
        }

        private void _op2Tracker_OperationCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs args)
        {
            if ((this.DoSomethingCompleted != null))
            {
                this.DoSomethingCompleted(this, args);
            }
        }

        private class op1Tracker : OperationTracker<IOverloadedServer, OverloadedClient, op1Delegate>
        {

            public op1Tracker(IOverloadedServer server, OverloadedClient client) : 
                    base(server, client)
            {
            }

            protected override void CallEndInvoke(op1Delegate d, System.IAsyncResult iar)
            {
                d.EndInvoke(iar);
            }
        }

        delegate void op1Delegate(int m);

        private class op2Tracker : OperationTracker<IOverloadedServer, OverloadedClient, op2Delegate>
        {

            public op2Tracker(IOverloadedServer server, OverloadedClient client) : 
                    base(server, client)
            {
            }

            protected override void CallEndInvoke(op2Delegate d, System.IAsyncResult iar)
            {
                d.EndInvoke(iar);
            }
        }

        delegate void op2Delegate(int m, int n);
    }
}

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