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

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


    public partial class Client : ClientBase<TestAssembly.IServer>
    {

        private CalculateTracker _calculateTracker;

        public Client(TestAssembly.IServer server) : 
                base(server)
        {
            this._calculateTracker = new CalculateTracker(this.server, this);
            this._calculateTracker.OperationCompleted += new AsyncGen.AsyncCompletedEventHandler<double>(this._calculateTracker_OperationCompleted);
            this._calculateTracker.CalculationProgressChanged += new AsyncGen.ProgressChangedEventHandler<double>(this._calculateTracker_CalculationProgressChanged);
        }

        public event AsyncGen.AsyncCompletedEventHandler<double> CalculationComplete;

        public event AsyncGen.ProgressChangedEventHandler<double> CalculationProgressChanged;

        public double Calculate(double argument, object userState)
        {
            double value;
            this._calculateTracker.CreateOperation(userState);
            try
            {
                value = this.server.Calculate(argument, userState, this._calculateTracker);
            }
            finally
            {
                this._calculateTracker.CompleteOperation(userState);
            }
            return value;
        }

        public void CalculateAsync(double argument, object userState)
        {
            this._calculateTracker.CreateOperation(userState);
            CalculateDelegate d = new CalculateDelegate(this.server.Calculate);
            d.BeginInvoke(argument, userState, this._calculateTracker, new System.AsyncCallback(this._calculateTracker.PostOperationCompleted), userState);
        }

        public void CalculateAsyncCancel(object userState)
        {
            if (this._calculateTracker.TryCancelOperation(userState))
            {
                return;
            }
            throw new System.ArgumentException();
        }

        private void _calculateTracker_CalculationProgressChanged(object sender, AsyncGen.ProgressChangedEventArgs<double> args)
        {
            if ((this.CalculationProgressChanged != null))
            {
                this.CalculationProgressChanged(this, args);
            }
        }

        private void _calculateTracker_OperationCompleted(object sender, AsyncGen.AsyncCompletedEventArgs<double> args)
        {
            if ((this.CalculationComplete != null))
            {
                this.CalculationComplete(this, args);
            }
        }

        private class CalculateTracker : OperationTracker<IServer, Client, CalculateDelegate, double>, TestAssembly.IServerCallbacks
        {

            public CalculateTracker(IServer server, Client client) : 
                    base(server, client)
            {
            }

            public event AsyncGen.ProgressChangedEventHandler<double> CalculationProgressChanged;

            protected override void CallEndInvoke(CalculateDelegate d, System.IAsyncResult iar, out double output)
            {
                output = d.EndInvoke(iar);
            }

            protected virtual void OnCalculationProgressChanged(object args)
            {
                if ((this.CalculationProgressChanged != null))
                {
                    this.CalculationProgressChanged(this.client, ((AsyncGen.ProgressChangedEventArgs<double>)(args)));
                }
            }

            bool IServerCallbacks.ReportCalculationProgress(double approximateResult, int percentage, object userState)
            {
                this.PostProgress<double>(new System.Threading.SendOrPostCallback(this.OnCalculationProgressChanged), percentage, approximateResult, userState);
                return this.IsOperationCancelled(userState);
            }
        }

        delegate double CalculateDelegate(double argument, object userState, TestAssembly.IServerCallbacks callbacks);
    }
}

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