Click here to Skip to main content
15,895,799 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.5K   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 EventBasedAsync
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using AsyncGen;


    public partial class Proxy : ClientBase<EventBasedAsync.IServer>
    {

        private DoSomethingTracker _dosomethingTracker;

        public event AsyncCompletedEventHandler<System.Int32> DoSomethingCompleted
        {
            add
            {
                this._dosomethingTracker.OperationCompleted += value;
            }
            remove
            {
                this._dosomethingTracker.OperationCompleted -= value;
            }
        }

        public event ProgressChangedEventHandler DoSomethingProgressChanged
        {
            add
            {
                this._dosomethingTracker.DoSomethingProgressChanged += value;
            }
            remove
            {
                this._dosomethingTracker.DoSomethingProgressChanged -= value;
            }
        }

        public Proxy(EventBasedAsync.IServer server)
            :
                base(server)
        {
            this._dosomethingTracker = new DoSomethingTracker(this.server, this);
        }

        public int DoSomething(int n, object userState)
        {
            int value;
            this._dosomethingTracker.CreateOperation(userState);
            try
            {
                value = this.server.DoSomething(n, userState, this._dosomethingTracker);
            }
            finally
            {
                this._dosomethingTracker.CompleteOperation(userState);
            }
            return value;
        }

        public void DoSomethingAsync(int n, object userState)
        {
            this._dosomethingTracker.CreateOperation(userState);
            DoSomethingDelegate d = new DoSomethingDelegate(this.server.DoSomething);
            d.BeginInvoke(n, userState, this._dosomethingTracker, new System.AsyncCallback(this._dosomethingTracker.PostOperationCompleted), userState);
        }

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

        private class DoSomethingTracker : OperationTracker<IServer, Proxy, DoSomethingDelegate, int>, EventBasedAsync.IDoSomethingCallbacks
        {

            public DoSomethingTracker(IServer server, Proxy client)
                :
                    base(server, client)
            {
            }

            public event ProgressChangedEventHandler DoSomethingProgressChanged;

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

            protected virtual void OnDoSomethingProgressChanged(object args)
            {
                if ((this.DoSomethingProgressChanged != null))
                {
                    this.DoSomethingProgressChanged(this.client, ((ProgressChangedEventArgs)(args)));
                }
            }

            void IDoSomethingCallbacks.ReportProgress(int progressPercentage, object userState, out bool cancelRequested)
            {
                this.PostProgress(new System.Threading.SendOrPostCallback(this.OnDoSomethingProgressChanged), progressPercentage, userState);
                cancelRequested = this.IsOperationCancelled(userState);
            }
        }

        delegate int DoSomethingDelegate(int n, object userState, EventBasedAsync.IDoSomethingCallbacks 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