Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Automatic Implementation of the Event-Based Asynchronous Pattern

, 26 Nov 2008 CPOL
Implement the event-based asynchronous pattern automatically with this code generator
asyncgen_demo.zip
asyncgen_demo
ClassLibrary1
bin
Debug
ClassDiagram.cd
Properties
Client
bin
Debug
Properties
Settings.settings
Server
bin
Debug
Properties
asyncgen_src.zip
asyncgen_src
AsyncClientGeneratorLib
asyncgen.snk
ClassDiagram.cd
fxReflection.proj
Properties
Properties
TestAssembly
Properties
TestClientCpp
app.ico
TestClientCSharp
Properties
TestClientVB
My Project
Application.myapp
Settings.settings
#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(this->server, &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(this->server, &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)

Share

About the Author

Ron A Inbar
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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 26 Nov 2008
Article Copyright 2008 by Ron A Inbar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid