Task Parallel Libraray vs Threading

1. Create a Console Application in VS2010 name TaskParallelLibraray 2. Include Following Name Spaces in Program.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Threading.Tasks;

3 Write Below code inside Class Program in Program.CS
static void Main(string[] args)
   {
      try
       {
          // CREATE A TREE WITH ~1023 NODES
          Tree tree = Tree.CreateSomeTree(10, 1);

          // START THE STOP WATCH
          Stopwatch sw = Stopwatch.StartNew();
                
          //WALK THE TREE
          WalkTree(tree);

          // GET THE TIME ELAPSED
           Console.WriteLine("Time Elapsed = " + sw.ElapsedMilliseconds.ToString());
                
          // WAIT TO SEE RESULTS
          Console.ReadLine();
          }
          catch (Exception ex)
          {
                Console.WriteLine(ex.Message);
                Console.Read();
          }
       }

        // WALK THE TREE USING RECURSION
        static void WalkTree(Tree tree)
        {
            //RETURN WHEN THERE ARE NO MORE CHILDRENS
            if (tree == null) return;

            #region USING TRADITIONAL SEQUENTIAL APPROACH

            WalkTree(tree.Left);
            WalkTree(tree.Right);

            #endregion

            #region USING THREADING

            // ************ PROCESS EVERY NODE USING THREADING
            // THIS MAY GIVE FOLLOWING EXCEPTION
            // An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll
            // BECAUSE WHENEVER NEW THREAD CREATED IT REQUIRE TO STORE PREVIOS THREAD STATE WHICH IS MORE MEMORY CONSUMING IN THIS EXAMPLE

            //Thread left = new Thread(() => WalkTree(tree.Left));
            //left.Start();
            //Thread right = new Thread(() => WalkTree(tree.Right));
            //right.Start();

            //// WAIT/JOIN ON BOTH OF THEM TO COMPLETE
            //left.Join();
            //right.Join();

            //************ Process every node using thread - End

            #endregion

            #region USING TASKS PARALLEL LIBRARY THAT IS USING TASK

            //Task left = new Task(() => WalkTree(tree.Left));
            //left.Start();
            //Task right = new Task(() => WalkTree(tree.Right));
            //right.Start();
            //left.Wait();
            //right.Wait();

            #endregion

            ProcessItem(tree.Data);
        }

        // SPINWAIT PUTS PROCESSOR IN TIGHT LOOP, WITH LOOP COUNT SPECIFIED IN ITERATION  PARAMETER
        private static int ProcessItem(int p)
        {
            // THIS IS WHERE COMPUTE/BUSINESS LOGIC GOES
            Thread.SpinWait(4000000); // CONTRAST WITH THREAD.SLEEP
            return p; // not used
        }


4. Add Class name Tree.cs and paste following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaskParallelLibraray
{
    public class Tree
    {
        public Tree Left = null;
        public Tree Right = null;

        public int Data = 0;

        // Tree of depth 3 and start of 1
        // each node will have children left and right
        // value will increase based on depth
        //       1
        //   2       2
        // 3   3   3   3
        //4 4 4 4 4 4 4 4
        internal static Tree CreateSomeTree(int depth, int start)
        {
            Tree root = new Tree();
            root.Data = start;

            if (depth > 0)
            {
                root.Left = CreateSomeTree(depth - 1, start + 1);
                root.Right = CreateSomeTree(depth - 1, start + 1);
            }
            return root;
        }
    }
}
5. Run this program in a machine having dual core or core to duo processor and c the out put Almost all our servers are having multicore processor :D so performance will be great on servers rather than out Desktops Reference :MSDN

posted under , , | 0 Comments

Must Know Sites.

Hello All,

This post is regarding the variety of powerful online options available to developers for development.I don't no how many of you have came across the situation like
1. I want to check this oops concept but i don't have compiler or
2. I want to run this jquery script or check css effect.
3. I want to create gradient using css .....
4. Online Photoshop editing 
 and many more.Actually i faced some of above mentioned problems and asked "Google Daddy" for help."Google Daddy" provided many options but i will refer you the following options.Many of these options are really worth to know.I am also trying to highlight benefits of using below sites. 

Tutorial Sites For Developers
Functionx
I have seen many people visiting W3Schools for learn the basic things.But once they complete the basic part another round of goggling get started.So all you people who likes development and looking for some good resource site, then functionx is a good option.In function X you will find the most of the content related to Microsoft Technologies.So any beginner looking for tutorials related to Microsoft Technologies visit this site.

Highlights: 
1. Good Content related to Microsoft Technologies like C#,Visual Basic,Ms Sql
2. Ready made developed projects with source are also provided and free of    cost to download
3. Good site for beginners as well for experience guys. 
Lacking in
1. I think site is lacking in web materials like creating web servies or css or jquery.
2.Site need to be redesign

W3Schools.
W3schools is very good site for beginners to start learning.Best think i like about W3schools is the variety.You have Asp tutorial, Php, Javscript,Jquery,Sql.Also user have "Try It Yourself" option which is good option if user want to try something.
Highlights
1.I would say this is very good site for beginners.
2.Variety of tutorials available.
3.Prepare new user for next level.
Lacking In
1.I think W3schools is lacking in the advance tutorial part.User will get the latest but not the advanced one.

 
C-Sharp Corner
Csharp corner is not a kind of above sites where user will find introductory tutorials.C Sharp corner is a site for user who have some development knowledge.In this site user will find the variety of tutorials like using speech Sdk or creating custom controls or working with share point or silverlight.
Highlights
1.Great collection of tutorials of Microsoft technologies
2.Sign up up is easy
3.Covers almost all Microsoft technologies
Lacking In
1.only related to Microsoft Development

 
CodeProject
This is another very famous site among developers for tutorials and porjects source code.Again,"Codeproject" consists variety of tutorials from basic to advanced level along with source code.Best part about Codeproject is its has tutorials of different programming language.


Highlights
1.Great collection of tutorials of Different technologies
2.Sign up up is easy

Older Posts

We have seen by


Welcome to iambeginner ......
I m not a well known developer or specialist of something. I m just normal learner who learns a lot from internet and trying to serve it to others.

    Followers

    Translate


Recent Comments