Click here to Skip to main content
15,891,843 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
#pragma once

#using <mscorlib.dll>

using namespace System::Security::Permissions;
[assembly:SecurityPermissionAttribute(SecurityAction::RequestMinimum, SkipVerification=false)];
namespace TestAssembly {
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Diagnostics;
    using namespace AsyncGen;
    using namespace System;
    ref class OverloadedClient;
    
    
    public ref class OverloadedClient : public ClientBase<TestAssembly::IOverloadedServer^ >
    {
        private : ref class op1Tracker;
        private : ref class op2Tracker;
        
        private: TestAssembly::OverloadedClient::op1Tracker^  _op1Tracker;
        
        private: TestAssembly::OverloadedClient::op2Tracker^  _op2Tracker;
        
        private : delegate System::Void op1Delegate(System::Int32 m);
        
        private : delegate System::Void op2Delegate(System::Int32 m, System::Int32 n);
        
        public: event System::ComponentModel::AsyncCompletedEventHandler^  DoSomethingCompleted;
        
        public: OverloadedClient(TestAssembly::IOverloadedServer^  server);
        public: System::Void DoSomething(System::Int32 m);
        
        public: System::Void DoSomethingAsync(System::Int32 m);
        
        public: System::Void DoSomethingAsyncCancel();
        
        private: System::Void _op1Tracker_OperationCompleted(System::Object^  sender, System::ComponentModel::AsyncCompletedEventArgs^  args);
        
        public: System::Void DoSomething(System::Int32 m, System::Int32 n);
        
        public: System::Void DoSomethingAsync(System::Int32 m, System::Int32 n);
        
        private: System::Void _op2Tracker_OperationCompleted(System::Object^  sender, System::ComponentModel::AsyncCompletedEventArgs^  args);
        
        private : ref class op1Tracker : public OperationTracker<IOverloadedServer^, TestAssembly::OverloadedClient^, TestAssembly::OverloadedClient::op1Delegate^ >
        {
            
            public: op1Tracker(IOverloadedServer^  server, TestAssembly::OverloadedClient^  client);
            protected: virtual System::Void CallEndInvoke(TestAssembly::OverloadedClient::op1Delegate^  d, System::IAsyncResult^  iar) override;
        };
        
        private : ref class op2Tracker : public OperationTracker<IOverloadedServer^, TestAssembly::OverloadedClient^, TestAssembly::OverloadedClient::op2Delegate^ >
        {
            
            public: op2Tracker(IOverloadedServer^  server, TestAssembly::OverloadedClient^  client);
            protected: virtual System::Void CallEndInvoke(TestAssembly::OverloadedClient::op2Delegate^  d, System::IAsyncResult^  iar) override;
        };
    };
}
namespace TestAssembly {
    
    
    inline OverloadedClient::OverloadedClient(TestAssembly::IOverloadedServer^  server) : 
            ClientBase<TestAssembly::IOverloadedServer^ >(server)
    {
        this->_op1Tracker = (gcnew TestAssembly::OverloadedClient::op1Tracker(this->server, this));
        this->_op1Tracker->OperationCompleted += gcnew System::ComponentModel::AsyncCompletedEventHandler(this, &TestAssembly::OverloadedClient::_op1Tracker_OperationCompleted);
        this->_op2Tracker = (gcnew TestAssembly::OverloadedClient::op2Tracker(this->server, this));
        this->_op2Tracker->OperationCompleted += gcnew System::ComponentModel::AsyncCompletedEventHandler(this, &TestAssembly::OverloadedClient::_op2Tracker_OperationCompleted);
    }
    
    inline System::Void OverloadedClient::DoSomething(System::Int32 m)
    {
        this->_op1Tracker->CreateOperation();
        try
        {
            this->server->DoSomething(m);
        }
        finally
        {
            this->_op1Tracker->CompleteOperation();
        }
    }
    
    inline System::Void OverloadedClient::DoSomethingAsync(System::Int32 m)
    {
        this->_op1Tracker->CreateOperation();
        TestAssembly::OverloadedClient::op1Delegate^  d = gcnew TestAssembly::OverloadedClient::op1Delegate((cli::safe_cast<TestAssembly::IOverloadedServer^  >(this->server)), &TestAssembly::IOverloadedServer::DoSomething);
        d->BeginInvoke(m, gcnew System::AsyncCallback(this->_op1Tracker, &TestAssembly::OverloadedClient::op1Tracker::PostOperationCompleted), 
            nullptr);
    }
    
    inline System::Void OverloadedClient::DoSomethingAsyncCancel()
    {
        if (this->_op1Tracker->TryCancelOperation())
        {
            return;
        }
        if (this->_op2Tracker->TryCancelOperation())
        {
            return;
        }
        throw (gcnew System::ArgumentException());
    }
    
    inline System::Void OverloadedClient::_op1Tracker_OperationCompleted(System::Object^  sender, System::ComponentModel::AsyncCompletedEventArgs^  args)
    {
        this->DoSomethingCompleted(this, args);
    }
    
    inline System::Void OverloadedClient::DoSomething(System::Int32 m, System::Int32 n)
    {
        this->_op2Tracker->CreateOperation();
        try
        {
            this->server->DoSomething(m, n);
        }
        finally
        {
            this->_op2Tracker->CompleteOperation();
        }
    }
    
    inline System::Void OverloadedClient::DoSomethingAsync(System::Int32 m, System::Int32 n)
    {
        this->_op2Tracker->CreateOperation();
        TestAssembly::OverloadedClient::op2Delegate^  d = gcnew TestAssembly::OverloadedClient::op2Delegate((cli::safe_cast<TestAssembly::IOverloadedServer^  >(this->server)), &TestAssembly::IOverloadedServer::DoSomething);
        d->BeginInvoke(m, n, gcnew System::AsyncCallback(this->_op2Tracker, &TestAssembly::OverloadedClient::op2Tracker::PostOperationCompleted), 
            nullptr);
    }
    
    inline System::Void OverloadedClient::_op2Tracker_OperationCompleted(System::Object^  sender, System::ComponentModel::AsyncCompletedEventArgs^  args)
    {
        this->DoSomethingCompleted(this, args);
    }
    
    
    inline OverloadedClient::op1Tracker::op1Tracker(IOverloadedServer^  server, TestAssembly::OverloadedClient^  client) : 
            OperationTracker<IOverloadedServer^, TestAssembly::OverloadedClient^, TestAssembly::OverloadedClient::op1Delegate^ >(server, 
                client)
    {
    }
    
    inline System::Void OverloadedClient::op1Tracker::CallEndInvoke(TestAssembly::OverloadedClient::op1Delegate^  d, System::IAsyncResult^  iar)
    {
        d->EndInvoke(iar);
    }
    
    
    inline OverloadedClient::op2Tracker::op2Tracker(IOverloadedServer^  server, TestAssembly::OverloadedClient^  client) : 
            OperationTracker<IOverloadedServer^, TestAssembly::OverloadedClient^, TestAssembly::OverloadedClient::op2Delegate^ >(server, 
                client)
    {
    }
    
    inline System::Void OverloadedClient::op2Tracker::CallEndInvoke(TestAssembly::OverloadedClient::op2Delegate^  d, System::IAsyncResult^  iar)
    {
        d->EndInvoke(iar);
    }
}

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