Click here to Skip to main content
15,888,065 members
Articles / Programming Languages / C#

NParallel, A Small Parallel Execution Library

Rate me:
Please Sign up or sign in to vote.
4.06/5 (30 votes)
19 Dec 2007CPOL9 min read 68.7K   606   60  
A Simple Library which allows you to write asynchronous code easily, almost in a synchronous pattern.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Diagnostics;

namespace NParallel.Core
{
    public delegate T TDelegate<T>();
    public delegate void TResultDel<T>(T result);
    public delegate void DelegateVoid();

    public enum NMarshal
    {
        Manual,
        Auto
    }
    
    public abstract class INResult 
    {
        protected bool done;
        protected IAsyncResult ar;
        protected NMarshal marshal = NMarshal.Auto;

        public NMarshal Marshal 
        {
            get 
            {
                return marshal;
            }
            set 
            {
                marshal = value;
            }
        }

        public INResult(IAsyncResult par) 
        {
            ar = par;
        }

        public bool Done
        {
            get
            {
                return done;
            }
            set
            {
                done = value;
            }
        }

        public bool IsDone()
        {
            return ar.IsCompleted;
        }

        public IAsyncResult Ar
        {
            get
            {
                return ar;
            }
            set
            {
                ar = value;
            }
        }

        public void AutoCallback(IAsyncResult tar) 
        {
            if (marshal == NMarshal.Auto && ar == tar)
            {
                Complete();
                DoCallback();
            }
        }

        public abstract void Complete();
        public abstract object GetCaller();
        public abstract void DoCallback();
    }

    public class NResult<T> : INResult
    {
        T val;
        TDelegate<T> caller = null;
        TResultDel<T> callback = null;
        
        public NResult(IAsyncResult ar , TDelegate<T> pcaller , TResultDel<T> pcallback)
            : base(ar)
        {
            caller = pcaller;
            callback = pcallback;
        }

        public NResult(TDelegate<T> pcaller, TResultDel<T> pcallback)
            : base(null)
        {
            caller = pcaller;
            callback = pcallback;
        }

        public TDelegate<T> Caller
        {
            set
            {
                caller = value;
            }
        }

        public TResultDel<T> Callback
        {
            set
            {
                callback = value;
            }
        }

        public T Value 
        {
            get 
            {
                if (! done)
                {
                    Wait();
                }
                
                return val;
            }
            set 
            {
                val = value;
            }
        }

        public T Wait(/* int milli-sec-to-wait */) 
        {
            lock (this)
            {
                if (done)
                {
                    return val;
                }

                if (ar == null)
                {
                    throw new ArgumentException("The result waited on is NULL");
                }

                object obj = ((AsyncResult)ar).AsyncDelegate;
                Debug.Assert(obj == caller);

                val = caller.EndInvoke(ar);
                //val = (T)Reflection.InvokeMethod(obj, "EndInvoke", ar);
                done = true;
                return val;
            }
        }

        public override void Complete() 
        {
            Wait();
        }

        public override object GetCaller() 
        {
            return caller;
        }

        public override void DoCallback() 
        {
            if (callback != null)
            {
                callback(Value);    
            }
            
        }
    }

    public class NResult : INResult
    {
        DelegateVoid caller = null;
        DelegateVoid callback = null;

        public NResult(IAsyncResult ar, DelegateVoid pcaller, DelegateVoid pcallback) 
            : base(ar)
        {
            caller = pcaller;
            callback = pcallback;
        }

        public NResult(DelegateVoid pcaller, DelegateVoid pcallback)
            : base(null)
        {
            caller = pcaller;
            callback = pcallback;
        }

        public DelegateVoid Caller
        {
            set
            {
                caller = value;
            }
        }

        public DelegateVoid Callback
        {
            set
            {
                callback = value;
            }
        }

        public void Wait(/* int milli-sec-to-wait */)
        {
            lock (this)
            {
                if (done)
                {
                    return;
                }

                if (ar == null)
                {
                    throw new ArgumentException("The result waited on is NULL");
                }

                object obj = ((AsyncResult)ar).AsyncDelegate;

                Debug.Assert(obj == caller);

                caller.EndInvoke(ar);
                done = true;
            }
        }

        public override void Complete()
        {
            Wait();
        }

        public override object GetCaller()
        {
            return caller;
        }

        public override void DoCallback()
        {
            if (callback != null)
            {
                callback();
            }
        }
    }
}

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)
China China
Leaf is a software developer based in ShangHai China.
My major programming languages are C#/C++.
I am very interested in distributed system design and rich client development.
Current I am working on NParallel.

Comments and Discussions