.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Threading >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

A Simple Multithreaded Application

Posted By:Abhisek Panda       Posted Date: November 27, 2009    Points: 10    Category: Threading    URL: http://www.dotnetspark.com  
 


A Simple Multithreaded Application:-

To build a multi-threaded application create a console application that allows the user to choose to work with a single thread or two threads. In the application we have to make use of System.Threading namespace. See the below application,
using System;
using System.Threading;
using System.Windows.Forms;

namespace SimpleMultiThreadApp
{
public class Printer
{
public void PrintNumbers()
{
// Display Thread information.
Console.WriteLine(">>> {0} is executing PrintNumbers()", Thread.CurrentThread.Name);
// Print the numbers
Console.Write("Your numbers: ");
for(int i = 0; i < 10; i++)
{
Console.Write(i + ", ");
Thread.Sleep(2000);//Will wait for two seconds to print the second number
}
Console.WriteLine();
}
}

public class MultiThread
{
static void Main(string[] args)
{
Console.WriteLine("A Simple Multithreading Application\n");
Console.Write("Do you want [1] or [2] threads? ");
string count = Console.ReadLine();
// Name the current thread.
Thread primaryThread = Thread.CurrentThread;
primaryThread.Name = "Primary";

// Display Thread information.
Console.WriteLine(">>> {0} is executing Main()",
Thread.CurrentThread.Name);
// Make Printer class object.
Printer p = new Printer();

switch(count)
{
case "1":
p.PrintNumbers();
break;

case "2":
//Make the secondary thread.
Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers));
backgroundThread.Name = "Secondary";
backgroundThread.Start();//Will start the secondary thread
break;

default:
Console.WriteLine("Entered wrong Information, you get 1 thread by default.");
goto case "1";
}

// Display the message box
MessageBox.Show("Busy Now!", "Working on main thread..");
Console.ReadLine();
}
}
}

Description:-


First we are declaring a class to perform the work of the secondary thread. The method will just print a sequence of numbers with an time interval of two seconds.
public class Printer
{
public void PrintNumbers()
{
// Display Thread information.
Console.WriteLine(">>> {0} is executing PrintNumbers()", Thread.CurrentThread.Name);
//Print the numbers
Console.Write("Your numbers: ");
for(int i = 0; i < 10; i++)
{
Console.Write(i + ", ");
Thread.Sleep(2000);//Will wait for two seconds to print the second number
}
Console.WriteLine();
}
}

          Inside the Main() we are prompting the user to enter his request oh single or two threads. If the user want a single thread just simply invoke the PrintNumber() method within the primary thread. If the user wants two threads then create a ThreadStart delegate that points to PrintNumber(). Pass this delegate object to the constructor of a new Thread object and callthe Start() to inform CIL that this thread is ready to execute.

Now we set a reference to the System.Windows.Forms namespace and displaying a message box within the Main() using MessageBox.Show().

          When you run the application using a single thread, the message box will be displayed after the entire sequence of no. is printed. If you will run the application using two threads then the message box will be displayed instantly, showing that another thread is responsible for printing the numbers.


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend