Check out and contribute to CodePedia, the wiki for developers.
*/

Other Views

corner
*/

C# School - Multithreading - Lesson #14 - Page 1

                     Next Page



Multithreading in C#

If you are new to C# School
This is the 14th in the series of lessons of our C# School. The C# School is a kind of interactive learning platform where those who want to learn .NET with C# can find help and support. With one issue a week, describing some areas of the C# Programming Language with the Microsoft .Net Platform, this is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue, which the reader is expected to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purposes. There is also a dedicated message board attached with the school, where you can ask questions about the article, and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author ( farazrasheed@acm.org)or the WEBMASTER ( info@programmersheaven.com).

For previous lessons: Lesson Plan
Today we will learn how to achieve multithreading in C# and .Net. We will start out by looking at what multithreading is and why we need it. Later we will demonstrate how we can implement multithreading in our C# application. Finally, we will learn about the different issues regarding thread synchronization and how C# handles them.

What is Multithreading
Multithreading is a feature provided by the operating system that enables your application to have more than one execution path at the same time. We are all used to Windows' multitasking abilities, which allow us to execute more than one application at the same time. Just right now I am writing the 14th lesson of the Programmers Heaven's C# School in Microsoft Word, listening to my favorite songs in WinAmp and downloading a new song using Internet Download Manager. In a similar manner, we may use multithreading to run different methods of our program at the same time. Multithreading is such a common element of today's programming that it is difficult to find windows applications that don't use it. For example, Microsoft Word takes user input and displays it on the screen in one thread while it continues to check spelling and grammatical mistakes in the second thread, and at the same time the third thread saves the document automatically at regular intervals. In a similar manner, WinAmp plays music in one thread, displays visualizations in the second and takes user input in the third. This is quite different from multitasking as here a single application is doing multiple tasks at the same time, while in multitasking different applications execute at the same time.

Author's Note: When we say two or more applications or threads are running at the same time, we mean that they appear to execute at the same time, e.g. without one waiting for the termination of the other before starting. Technically, no two instructions can execute together at the same time on a single processor system (which most of us use). What the operating system does is divides the processor's execution time amongst the different applications (multitasking) and within an application amongst the different threads (multithreading).

Just consider the following small program:

namespace CSharpSchool
{
	class Test
	{
		static void Main()
		{
			Fun1();
			Fun2();
			Console.WriteLine("End of Main()");
		}
		public static void Fun1()
		{
			for(int i=1; i<=5; i++)
			{
				Console.WriteLine("Fun1() writes: {0}", i);
			}
		}
		public static void Fun2()
		{
			for(int i=10; i>=6; i--)
			{
				Console.WriteLine("Fun2() writes: {0}", i);
			}
		}
	}
}


The output of the program is:

Fun1() writes: 1
Fun1() writes: 2
Fun1() writes: 3
Fun1() writes: 4
Fun1() writes: 5
Fun2() writes: 10
Fun2() writes: 9
Fun2() writes: 8
Fun2() writes: 7
Fun2() writes: 6
End of Main()
Press any key to continue


As we can see, the method Fun2() only started its execution when Fun1() had completed its execution. This is because when a method gets called, the execution control transfers to that method, and when the method returns the execution starts from the very next line of the code that called the method, i.e., the program implicitly has only one execution path. Using multithreading, we can define multiple concurrent execution paths within our program called threads. For example, we can use threads so that the two methods Fun1() and Fun2() may execute without waiting for each other to terminate.


                     Next Page





corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.
Resource Listings