Click here to Skip to main content
15,881,380 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
#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 Client;
    
    
    public ref class Client : public ClientBase<TestAssembly::IServer^ >
    {
        private : ref class CalculateTracker;
        
        private: TestAssembly::Client::CalculateTracker^  _calculateTracker;
        
        private : delegate System::Double CalculateDelegate(System::Double argument, System::Object^  userState, TestAssembly::IServerCallbacks^  callbacks);
        
    public: event AsyncGen::AsyncCompletedEventHandler<System::Double >^  CalculationComplete;
        
    public: event AsyncGen::ProgressChangedEventHandler<System::Double >^  CalculationProgressChanged;
        
        public: Client(TestAssembly::IServer^  server);
        public: System::Double Calculate(System::Double argument, System::Object^  userState);
        
        public: System::Void CalculateAsync(System::Double argument, System::Object^  userState);
        
        public: System::Void CalculateAsyncCancel(System::Object^  userState);
        
    private: System::Void _calculateTracker_CalculationProgressChanged(System::Object^  sender, AsyncGen::ProgressChangedEventArgs<System::Double >^  args);
        
    private: System::Void _calculateTracker_OperationCompleted(System::Object^  sender, AsyncGen::AsyncCompletedEventArgs<System::Double >^  args);
        
        private : ref class CalculateTracker : public OperationTracker<IServer^, TestAssembly::Client^, TestAssembly::Client::CalculateDelegate^, System::Double >, 
            public TestAssembly::IServerCallbacks
        {
            
        public: event AsyncGen::ProgressChangedEventHandler<System::Double >^  CalculationProgressChanged;
            
            public: CalculateTracker(IServer^  server, TestAssembly::Client^  client);
            protected: virtual System::Void CallEndInvoke(TestAssembly::Client::CalculateDelegate^  d, System::IAsyncResult^  iar, 
                        System::Double %output) override;
            
            protected: virtual System::Void OnCalculationProgressChanged(System::Object^  args);
            
        public: virtual System::Boolean ReportCalculationProgress(System::Double approximateResult, System::Int32 percentage, 
                        System::Object^  userState) sealed;
        };
    };
}
namespace TestAssembly {
    
    
    inline Client::Client(TestAssembly::IServer^  server) : 
            ClientBase<TestAssembly::IServer^ >(server)
    {
        this->_calculateTracker = (gcnew TestAssembly::Client::CalculateTracker(this->server, this));
        this->_calculateTracker->OperationCompleted += gcnew AsyncGen::AsyncCompletedEventHandler<System::Double >(this, &TestAssembly::Client::_calculateTracker_OperationCompleted);
        this->_calculateTracker->CalculationProgressChanged += gcnew AsyncGen::ProgressChangedEventHandler<System::Double >(this, &TestAssembly::Client::_calculateTracker_CalculationProgressChanged);
    }
    
    inline System::Double Client::Calculate(System::Double argument, System::Object^  userState)
    {
        System::Double __identifier(value);
        this->_calculateTracker->CreateOperation(userState);
        try
        {
            __identifier(value) = this->server->Calculate(argument, userState, this->_calculateTracker);
        }
        finally
        {
            this->_calculateTracker->CompleteOperation(userState);
        }
        return __identifier(value);
    }
    
    inline System::Void Client::CalculateAsync(System::Double argument, System::Object^  userState)
    {
        this->_calculateTracker->CreateOperation(userState);
        TestAssembly::Client::CalculateDelegate^  d = gcnew TestAssembly::Client::CalculateDelegate((cli::safe_cast<TestAssembly::IServer^  >(this->server)), &TestAssembly::IServer::Calculate);
        d->BeginInvoke(argument, userState, this->_calculateTracker, gcnew System::AsyncCallback(this->_calculateTracker, &TestAssembly::Client::CalculateTracker::PostOperationCompleted), 
            userState);
    }
    
    inline System::Void Client::CalculateAsyncCancel(System::Object^  userState)
    {
        if (this->_calculateTracker->TryCancelOperation(userState))
        {
            return;
        }
        throw (gcnew System::ArgumentException());
    }
    
    inline System::Void Client::_calculateTracker_CalculationProgressChanged(System::Object^  sender, AsyncGen::ProgressChangedEventArgs<System::Double >^  args)
    {
        this->CalculationProgressChanged(this, args);
    }
    
    inline System::Void Client::_calculateTracker_OperationCompleted(System::Object^  sender, AsyncGen::AsyncCompletedEventArgs<System::Double >^  args)
    {
        this->CalculationComplete(this, args);
    }
    
    
    inline Client::CalculateTracker::CalculateTracker(IServer^  server, TestAssembly::Client^  client) : 
            OperationTracker<IServer^, TestAssembly::Client^, TestAssembly::Client::CalculateDelegate^, System::Double >(server, 
                client)
    {
    }
    
    inline System::Void Client::CalculateTracker::CallEndInvoke(TestAssembly::Client::CalculateDelegate^  d, System::IAsyncResult^  iar, 
                System::Double %output)
    {
        output = d->EndInvoke(iar);
    }
    
    inline System::Void Client::CalculateTracker::OnCalculationProgressChanged(System::Object^  args)
    {
        this->CalculationProgressChanged(this->client, (cli::safe_cast<AsyncGen::ProgressChangedEventArgs<System::Double >^  >(args)));
    }
    
    inline System::Boolean Client::CalculateTracker::ReportCalculationProgress(System::Double approximateResult, System::Int32 percentage, 
                System::Object^  userState)
    {
        this->PostProgress<System::Double >(gcnew System::Threading::SendOrPostCallback(this, &TestAssembly::Client::CalculateTracker::OnCalculationProgressChanged), 
            percentage, approximateResult, userState);
        return this->IsOperationCancelled(userState);
    }
}

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