Working with Multiple Threads in C#

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.


Comments

Author: Abhisek Panda22 Nov 2009 Member Level: Gold   Points : 0

I want to know hwy points are reduced???????



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: