Skip Navigation Links

 CodeMatrixThe ultimate site for .NET programmers

Skip Navigation Links.    

Multithreading in C#

By default, a C# program has one thread. This thread executes the code in the program starting and ending with the Main method. Every command executed by Main--either directly or indirectly--is performed by the default, or primary thread, and this thread terminates when Main returns. However, auxiliary threads can be created and used to execute code in parallel with the primary thread. These threads are often called worker threads.

Worker threads can be used to perform time consuming or time critical tasks without tying up the primary thread. For example, worker threads are often used in server applications to fulfill incoming requests without waiting for the previous request to be complete. Worker threads are also used to perform "background" tasks in desktop applications so that the main thread--which drives user interface elements--remains responsive to user actions.

Multithreading solves problems with throughput and responsiveness, but it can also introduce resource-sharing issues such as deadlocks and race conditions. Multiple threads are best for tasks that require different resources such as file handles, network connections. Assigning multiple threads to a single resource is likely to cause synchronization issues, and having threads frequently blocked while waiting for other threads defeats the purpose of using multiple threads.

The following example demonstrates the use of Multithreading in C#.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace UsingThreads
{
    class SampleThread
    {
        public void ThreadTest()
        {
            // create a thread for the IncrementCounter
            Thread inc_thread = new Thread(
                new ThreadStart(IncrementCounter));

            // create a thread for the DecrementCounter
            Thread dec_thread = new Thread(
                new ThreadStart(DecrementCounter));

            // start the threads
            inc_thread.Start();
            dec_thread.Start();
        }

        // counts up to 500
        public void IncrementCounter()
        {
            for (int i = 0; i < 500; i++)
            {
                System.Console.WriteLine(
                    "IncrementCounter: {0}", i);
            }
        }

        // counts down from 500
        public void DecrementCounter()
        {
            for (int i = 500; i >= 0; i--)
            {
                System.Console.WriteLine(
                    "DecrementCounter: {0}", i);
            }
        }

        static void Main()
        {
            // make an instance of this class
            SampleThread t = new SampleThread();

            Console.WriteLine("Thread test starts");
            // run the thread
            t.ThreadTest();
        }
    }
}
Downloads
The source code in this article can be downloaded from below link. The code is complied with Microsoft Visual C# 2008.

Copyright © 2008 www.codematrix.net, All Rights Reserved. Disclaimer