Click here to Skip to main content
15,888,182 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.8K   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 NParallel.Core;
using System.Threading;

namespace Testcases
{
    delegate int NNN(int a, int b);
    class Program
    {
        static int GetResult(int a , int b) 
        {
            Thread.Sleep(2000);

            return a + b;
        }

        static void DummyMethod() 
        {
            for (int i = 0; i < 10; i++)
            {
                Console.Write("{0};" ,i);
                Thread.Sleep(1);
            }

            Console.WriteLine();
            
        }

        static void Calculate(int b) 
        {
            Console.WriteLine("{1} : Result of the method is {0}", b , Thread.CurrentThread.ManagedThreadId);
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Main Thread ID is {0}", Thread.CurrentThread.ManagedThreadId);
            NPOperations.Execute<int>
            (
                delegate 
                {
                    return Program.GetResult(99 , 88);
                },

                Calculate
                , NMarshal.Manual // this is especially useful for UI threads.
            );

            while (!NQueue.Instance.Empty)
            {
                NQueue.Instance.Update();
            }

            NResult<int> result3 = NPOperations.Execute<int>(
                delegate
                {
                    return Program.GetResult(3000, 2000);
                }
            );

            //NResult<int> result4 = NPOperations.Execute<int>( GetResult(3000, 2000));

            NResult r1 = NPOperations.Execute(delegate
            {
                Program.DummyMethod();
            });
            
            Console.WriteLine("test1");

            NResult r2 = NPOperations.Execute(delegate
            {
                Program.DummyMethod();
                Program.DummyMethod();
            });

            Console.WriteLine("test2");

            r1.Wait(); r2.Wait();

            NPOperations.Execute(delegate
            {
                Program.DummyMethod();
            });

            Console.WriteLine("test3");

            Console.WriteLine("This is the start of the test!!!");

            Console.WriteLine("result.Value is {0}", result3.Value);
            Console.WriteLine("This is the end of the test!!!");

            Console.ReadLine();
        }
    }
}

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