Click here to Skip to main content
15,886,110 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.5K   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.Linq;
using System.Text;

namespace Leaf.Parallel
{
    /// <summary>
    /// Class used to bridge methods together, execute a method before or after a normal routine
    /// or execute another method use return value of the first one(Continuous)
    /// </summary>
    public static class NBridge
    {
        #region PreExec: Execute a method before the normal routine
        public static Func<TResult> PreExec<TResult>(Func<TResult> normalRoutine, NVoidFunc preRoutine) 
        {
            return () =>
            {
                preRoutine();
                return normalRoutine();
            };
        }

        public static Func<TArgument1 , TResult> PreExec<TArgument1 , TResult>(Func<TArgument1 , TResult> normalRoutine, NVoidFunc preRoutine)
        {
            return (t1) =>
            {
                preRoutine();
                return normalRoutine(t1);
            };
        }

        public static Func<TArgument1, TArgument2, TResult> PreExec<TArgument1, TArgument2, TResult>(Func<TArgument1, TArgument2, TResult> normalRoutine, NVoidFunc preRoutine)
        {
            return (t1 , t2) =>
            {
                preRoutine();
                return normalRoutine(t1 , t2);
            };
        }

        public static Func<TArgument1, TArgument2, TArgument3, TResult> PreExec<TArgument1, TArgument2, TArgument3, TResult>(Func<TArgument1, TArgument2, TArgument3, TResult> normalRoutine, NVoidFunc preRoutine)
        {
            return (t1 ,t2 , t3) =>
            {
                preRoutine();
                return normalRoutine(t1 , t2 , t3);
            };
        }
        public static Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult> PreExec<TArgument1, TArgument2, TArgument3, TArgument4, TResult>(Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult> normalRoutine, NVoidFunc preRoutine)
        {
            return (t1, t2, t3,t4) =>
            {
                preRoutine();
                return normalRoutine(t1, t2, t3 , t4);
            };
        }
        #endregion

        #region PostExec : execute another methods after the nornal one
        public static Func<TResult> PostExec<TResult>(Func<TResult> normalRoutine, NVoidFunc postRoutine)
        {
            return () =>
            {
                TResult result = normalRoutine();
                postRoutine();
                return result;
            };
        }

        public static Func<TArgument1, TResult> PostExec<TArgument1, TResult>(Func<TArgument1, TResult> normalRoutine, NVoidFunc postRoutine)
        {
            return (t1) =>
            {
                TResult result = normalRoutine(t1);
                postRoutine();
                return result;
            };
        }

        public static Func<TArgument1, TArgument2, TResult> PostExec<TArgument1, TArgument2, TResult>(Func<TArgument1, TArgument2, TResult> normalRoutine, NVoidFunc postRoutine)
        {
            return (t1, t2) =>
            {
                TResult result = normalRoutine(t1, t2);
                postRoutine();
                return result;
            };
        }

        public static Func<TArgument1, TArgument2, TArgument3, TResult> PostExec<TArgument1, TArgument2, TArgument3, TResult>(Func<TArgument1, TArgument2, TArgument3, TResult> normalRoutine, NVoidFunc postRoutine)
        {
            return (t1, t2, t3) =>
            {
                TResult result = normalRoutine(t1, t2, t3);
                postRoutine();
                return result;
            };
        }
        public static Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult> PostExec<TArgument1, TArgument2, TArgument3, TArgument4, TResult>(Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult> normalRoutine, NVoidFunc postRoutine)
        {
            return (t1, t2, t3, t4) =>
            {
                TResult result = normalRoutine(t1, t2, t3, t4);
                postRoutine();
                return result;
            };
        }
        #endregion

        #region Bridge: Used to connect two methods.
        public static Func<TResult2> Bridge<TResult1 , TResult2>(Func<TResult1> firstFunc, Func<TResult1, TResult2> nextFunc) 
        {
            return () =>
            {
                TResult1 tempResult = firstFunc();
                return nextFunc(tempResult);
            };
        }

        public static Func<TArgument1, TResult2> Bridge<TArgument1, TResult1, TResult2>(Func<TArgument1, TResult1> firstFunc, Func<TResult1, TResult2> nextFunc)
        {
            return (t1) =>
            {
                TResult1 tempResult = firstFunc(t1);
                return nextFunc(tempResult);
            };
        }

        public static Func<TArgument1, TArgument2, TResult2> Bridge<TArgument1, TArgument2, TResult1, TResult2>(Func<TArgument1, TArgument2, TResult1> firstFunc, Func<TResult1, TResult2> nextFunc)
        {
            return (t1 , t2) =>
            {
                TResult1 tempResult = firstFunc(t1 , t2);
                return nextFunc(tempResult);
            };
        }

        public static Func<TArgument1, TArgument2, TArgument3, TResult2> Bridge<TArgument1, TArgument2, TArgument3, TResult1, TResult2>(Func<TArgument1, TArgument2, TArgument3, TResult1> firstFunc, Func<TResult1, TResult2> nextFunc)
        {
            return (t1 , t2 , t3) =>
            {
                TResult1 tempResult = firstFunc(t1 , t2 , t3);
                return nextFunc(tempResult);
            };
        }

        public static Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult2> Bridge<TArgument1, TArgument2, TArgument3, TArgument4, TResult1, TResult2>(Func<TArgument1, TArgument2, TArgument3, TArgument4, TResult1> firstFunc, Func<TResult1, TResult2> nextFunc)
        {
            return (t1 ,t2 ,t3 ,t4) =>
            {
                TResult1 tempResult = firstFunc(t1 , t2 , t3 , t4);
                return nextFunc(tempResult);
            };
        }

        #endregion
    }
}

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