Click here to Skip to main content
15,885,985 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 ClientWithMultipleOutputs;
    
    
    public ref class ClientWithMultipleOutputs : public ClientBase<TestAssembly::IServerWithMultipleOutputs^ >
    {
        public : value class ComputeOutput;
        public : value class IntermediateResults;
        private : ref class ComputeTracker;
        
        private: TestAssembly::ClientWithMultipleOutputs::ComputeTracker^  _computeTracker;
        
        private : delegate System::Double ComputeDelegate(System::Double %x, System::Double %y);
        
        public: event AsyncGen::AsyncCompletedEventHandler<TestAssembly::ClientWithMultipleOutputs::ComputeOutput >^  ComputeCompleted;
        
        public: event AsyncGen::ProgressChangedEventHandler<TestAssembly::ClientWithMultipleOutputs::IntermediateResults >^  ApproximateResultsAvailable;
        
        public: ClientWithMultipleOutputs(TestAssembly::IServerWithMultipleOutputs^  server);
        public: System::Double Compute(System::Double %x, System::Double %y);
        
        public: System::Void ComputeAsync(System::Double x);
        
        private: System::Void _computeTracker_ApproximateResultsAvailable(System::Object^  sender, AsyncGen::ProgressChangedEventArgs<TestAssembly::ClientWithMultipleOutputs::IntermediateResults >^  args);
        
        private: System::Void _computeTracker_OperationCompleted(System::Object^  sender, AsyncGen::AsyncCompletedEventArgs<TestAssembly::ClientWithMultipleOutputs::ComputeOutput >^  args);
        
        public : value class ComputeOutput
        {
            
            public: System::Double _ReturnValue;
            
            public: System::Double x;
            
            public: System::Double y;
        };
        
        public : value class IntermediateResults
        {
            
            public: System::Double __identifier(return);
            
            public: System::Double x;
            
            public: System::Double y;
        };
        
        private : ref class ComputeTracker : public OperationTracker<IServerWithMultipleOutputs^, TestAssembly::ClientWithMultipleOutputs^, TestAssembly::ClientWithMultipleOutputs::ComputeDelegate^, TestAssembly::ClientWithMultipleOutputs::ComputeOutput >, 
            public TestAssembly::IServerWithMultipleOutputsCallbacks
        {
            
            public: event AsyncGen::ProgressChangedEventHandler<TestAssembly::ClientWithMultipleOutputs::IntermediateResults >^  ApproximateResultsAvailable;
            
            public: ComputeTracker(IServerWithMultipleOutputs^  server, TestAssembly::ClientWithMultipleOutputs^  client);
            protected: virtual System::Void CallEndInvoke(TestAssembly::ClientWithMultipleOutputs::ComputeDelegate^  d, System::IAsyncResult^  iar, 
                        TestAssembly::ClientWithMultipleOutputs::ComputeOutput %output) override;
            
            protected: virtual System::Void OnApproximateResultsAvailable(System::Object^  args);
            
            public: virtual System::Void ReportApproximateResults(System::Int32 progress, System::Double __identifier(return), 
                        System::Double x, System::Double y) sealed;
        };
    };
}
namespace TestAssembly {
    
    
    inline ClientWithMultipleOutputs::ClientWithMultipleOutputs(TestAssembly::IServerWithMultipleOutputs^  server) : 
            ClientBase<TestAssembly::IServerWithMultipleOutputs^ >(server)
    {
        this->_computeTracker = (gcnew TestAssembly::ClientWithMultipleOutputs::ComputeTracker(this->server, this));
        this->_computeTracker->OperationCompleted += gcnew AsyncGen::AsyncCompletedEventHandler<TestAssembly::ClientWithMultipleOutputs::ComputeOutput >(this, &TestAssembly::ClientWithMultipleOutputs::_computeTracker_OperationCompleted);
        this->_computeTracker->ApproximateResultsAvailable += gcnew AsyncGen::ProgressChangedEventHandler<TestAssembly::ClientWithMultipleOutputs::IntermediateResults >(this, &TestAssembly::ClientWithMultipleOutputs::_computeTracker_ApproximateResultsAvailable);
    }
    
    inline System::Double ClientWithMultipleOutputs::Compute(System::Double %x, System::Double %y)
    {
        System::Double __identifier(value);
        this->_computeTracker->CreateOperation();
        try
        {
            __identifier(value) = this->server->Compute(x, y);
        }
        finally
        {
            this->_computeTracker->CompleteOperation();
        }
        return __identifier(value);
    }
    
    inline System::Void ClientWithMultipleOutputs::ComputeAsync(System::Double x)
    {
        this->_computeTracker->CreateOperation();
        System::Double y;
        TestAssembly::ClientWithMultipleOutputs::ComputeDelegate^  d = gcnew TestAssembly::ClientWithMultipleOutputs::ComputeDelegate((cli::safe_cast<TestAssembly::IServerWithMultipleOutputs^  >(this->server)), &TestAssembly::IServerWithMultipleOutputs::Compute);
        d->BeginInvoke(x, y, gcnew System::AsyncCallback(this->_computeTracker, &TestAssembly::ClientWithMultipleOutputs::ComputeTracker::PostOperationCompleted), 
            nullptr);
    }
    
    inline System::Void ClientWithMultipleOutputs::_computeTracker_ApproximateResultsAvailable(System::Object^  sender, AsyncGen::ProgressChangedEventArgs<TestAssembly::ClientWithMultipleOutputs::IntermediateResults >^  args)
    {
        this->ApproximateResultsAvailable(this, args);
    }
    
    inline System::Void ClientWithMultipleOutputs::_computeTracker_OperationCompleted(System::Object^  sender, AsyncGen::AsyncCompletedEventArgs<TestAssembly::ClientWithMultipleOutputs::ComputeOutput >^  args)
    {
        this->ComputeCompleted(this, args);
    }
    
    
    
    
    inline ClientWithMultipleOutputs::ComputeTracker::ComputeTracker(IServerWithMultipleOutputs^  server, TestAssembly::ClientWithMultipleOutputs^  client) : 
            OperationTracker<IServerWithMultipleOutputs^, TestAssembly::ClientWithMultipleOutputs^, TestAssembly::ClientWithMultipleOutputs::ComputeDelegate^, TestAssembly::ClientWithMultipleOutputs::ComputeOutput >(server, 
                client)
    {
    }
    
    inline System::Void ClientWithMultipleOutputs::ComputeTracker::CallEndInvoke(TestAssembly::ClientWithMultipleOutputs::ComputeDelegate^  d, 
                System::IAsyncResult^  iar, TestAssembly::ClientWithMultipleOutputs::ComputeOutput %output)
    {
        output.x = System::Double();
        output._ReturnValue = d->EndInvoke(output.x, output.y, iar);
    }
    
    inline System::Void ClientWithMultipleOutputs::ComputeTracker::OnApproximateResultsAvailable(System::Object^  args)
    {
        this->ApproximateResultsAvailable(this->client, (cli::safe_cast<AsyncGen::ProgressChangedEventArgs<TestAssembly::ClientWithMultipleOutputs::IntermediateResults >^  >(args)));
    }
    
    inline System::Void ClientWithMultipleOutputs::ComputeTracker::ReportApproximateResults(System::Int32 progress, System::Double __identifier(return), 
                System::Double x, System::Double y)
    {
        TestAssembly::ClientWithMultipleOutputs::IntermediateResults results = TestAssembly::ClientWithMultipleOutputs::IntermediateResults();
        results.__identifier(return) = __identifier(return);
        results.x = x;
        results.y = y;
        this->PostProgress<TestAssembly::ClientWithMultipleOutputs::IntermediateResults >(gcnew System::Threading::SendOrPostCallback(this, &TestAssembly::ClientWithMultipleOutputs::ComputeTracker::OnApproximateResultsAvailable), 
            progress, results);
    }
}

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