Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Wednesday, June 2, 2010

Threads example in C#.NET Complex Example

1.Create two namespaces.

Explanation: Go to file and click on create new project, from the list of options select 'Console Application'. I have named my console application as 'ThreadsExample'.Now you will see on the right, a solution explorer which displays your console application. Your solution has just one project. To add another project to your solution, right click on 'Solution 'ThreadsExample'' select Add-->Add New Project--> Select a console application and name it as 'Consulting Console'.Great good work. Now you have got 2 projects in your solution.
My idea is to display the mechanism of communicating from one console to another console.

2.Define First namespace as
//Open Program.cs in ThreadsExample project and Paste this code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsultingConsole;
using System.Threading;

namespace ThreadsExample
{
   public  class Program
    {
      
        static void Main(string[] args)
        {
            Console.WriteLine("Type the message you want to send to other console:");
            string inputfromC1 =  Console.ReadLine();
            object c1 = (object)inputfromC1;
          
            ThreadPool.QueueUserWorkItem(SendToConsole2,c1);
         
           Console.WriteLine("Console 1 is waiting for the key however i can still display stuff");
           Console.ReadKey();
           


        }
        public   static void  SendToConsole2(object _input)
        {
            string _strinput = (string)_input;
            _strinput = "Console-1 Says: " +  _input;
            Console.WriteLine("Executing Thread While Console 1 is waiting for key ");

            Console.WriteLine("Calling Console2:");
            ConsultingConsole.Program.o1 = (object)_strinput;
           
            ConsultingConsole.Program.Main();
            Console.ReadKey();

            Console.WriteLine("Console2 Has finished executing..cool");
            Console.ReadKey();
           

        }
      
      
   }
}

2.Define the other namespace as follows :
//Open program.cs in 'ConsultingConsole' project and paste this code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

 namespace ConsultingConsole
{
    public class Program
    {
       public static  object o1 = new object();
       public  static void Main()
        {
           
            ThreadPool.QueueUserWorkItem(RecvFromConsole1, o1);
           
        }

        public static  void RecvFromConsole1(object  _input)
        {
            string _strinput = (string) _input;
            Console.WriteLine( _strinput);
            Console.WriteLine("Finally Reached Console 2");
            Console.ReadKey();

        }

    }
}

3. To actual see it working, debug your application and check out how cool is threading!!!

Explanation : Let me explain something about threads before we execute the above example. Threads run in background as well as foreground. Imagine i have a multimedia application which plays audio and video. When the user inserts a DVD in his ROM while he is listening to music, the multimedia application will be checking the DVD for any multimedia content. However multimedia application cannot stop playing the music while checking the DVD. Both these tasks must be carried out simultaneously. While the user is listening to songs, software checks the DVD, if there is any content which can be played, then the it should display all the possible options to the user, all of these tasks must be done background. So to achieve multi tasking, we can use threads. So here in this example, the console-1 is waiting for input from user, during this time the method SendToConsole2(object _input) is run in the background. If you know how to debug, just check how amazing the whole idea is!!!, Else i know you wouldn't understand a thing.

4. Am going to update this post very soon. I will provide many simple threading example for you to understand real soon, so keep checking

Regards
Sameer Shaik

Free MCTS EXAM 70-536 training tutorials on this website, soon will be launching video tutorials for all the same topics
http://www.youtube.com/watch?v=2KOcypWILxA
 


No comments:

Post a Comment