SIGN UP MEMBER LOGIN:    
ARTICLE

Synchronized Threading in .NET

Posted by Ahmed Al Kayali Articles | Multithreading in C# December 11, 2002
Threads are a powerful abstraction for allowing parallelized operations: graphical updates can happen while another thread is performing computations, two threads can handle two simultaneous network requests from a single process, and the list goes on. Since threads are pretty simple to understand, conceptually, but, practically, they are a cause for programmatic headaches, I decided to write this program to describe how to make use of threads.
Reader Level:
Download Files:
 

Introduction

Threads are a powerful abstraction for allowing parallelized operations: graphical updates can happen while another thread is performing computations, two threads can handle two simultaneous network requests from a single process, and the list goes on. Since threads are pretty simple to understand, conceptually, but, practically, they are a cause for programmatic headaches, I decided to write this program to describe how to make use of threads.

Background

To understand this code completely, you need to have some basics on C# language. I used some form and controls that are easy to understand. You can refer to the MS Visual Studio Documentation to understand the System.Threading.Thread class and all its properties and methods.

Using the code

The project AutoChess.sln consists of three classes:

  1. Form1.cs
  2. King.cs
  3. Soldier.cs

This program, when executed, shows the previous grid. If one presses the (Start) button, those pieces start to move in random fashion. The King moves in any direction and the soldier moves in diagonals.Let's have a closer look at the code. We declared four threads, one for each piece:

Thread t1, t2, t3, t4;

Then we create our four threads:

t1 = new Thread(ThreadStart(MoveWhiteKing));
t2 =
new Thread(ThreadStart(MoveBlackKing));
t3 =
new Thread(ThreadStart(MoveWhiteSoldier));
t4 =
new Thread(ThreadStart(MoveBlackSoldier));

Where MoveWhiteKing,MoveBlackKing,... are delegates that refer to methods responsible for moving the pieces. And I decided also to create an object for each piece used. Those are:

WK = new WindowsApplication1.King (bs,WhiteKingPic , WhiteSoldierPic);
BK =
new WindowsApplication1.King (bs,BlackKingPic , BlackSoldierPic);
WS =
new WindowsApplication1.King (bs,WhiteSoldierPic ,WhiteKingPic,BlackKingPic );
BS =
new WindowsApplication1.King (bs,BlackSoldierPic ,BlackKingPic,WhiteKingPic );

To start the four threads, the following code is put in button1_Click(), which is the Start button:

private void button1_Click(object sender, System.EventArgs e)
{
this.button1.Enabled = false;
t1.Start();
t2.Start();
t3.Start();
t4.Start();
}

To understand more how each thread works let's have a closer look at the method referenced by the delegates. Let's take the MoveWhiteKing() as an example:

private void MoveWhiteKing ()
{
lock( synchronizeVariable )
{
while(true)
{
Thread.Sleep(1000);
Monitor.PulseAll( synchronizeVariable );
WK.MoveKing(bs);
Monitor.Wait( synchronizeVariable );
}
}
}

We use the synchronizeVariable object that is assigned in the beginning of the program:

public static Object synchronizeVariable = "locking variable";

This variable is used to guarantee a certain behavior so that no two pieces move together. This is possible since there is only one thread that can grab the synchronizeVariable and ,in turn, can be active at a time. Thread.Sleep(1000) causes the thread to be delayed by one second and then the thread notifies all other threads that the synchronizeVariable is with it and no thread can take the control. The thread calls the method for moving the piece according to some defined rules. Then, the current thread put itself into a Wait(), releasing the variable to allow other threads to grab it, and so on.

Conclusion

I hope this example will be useful for many. I noticed that there are few examples discussing threads, especially those with the synchronization characteristic. So, I tried to make this example as easy as possible to understand. I will feel glad if there is anyone wants to make suggestions or discussion on anything that I can help in.

Login to add your contents and source code to this article
share this article :
post comment
 

At a time 100's of users sends a message to the server. Here I am setting MAXThreads=25. If the users exceeds more than 25 I have to take the requests and put it in to the queue. If thread is available execute the operation until there is no process in process queue. Please help me!

Posted by ramesh p Aug 01, 2007

At a time 100's of users sends a message to the server. Here I am setting MAXThreads=25. If the users exceeds more than 25 I have to take the requests and put it in to the queue. If thread is available execute the operation until there is no process in process queue.

Posted by ramesh p Aug 01, 2007

did you got my previous message

Posted by AbdulKhader Mohammed Apr 22, 2007
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor