Home > DeveloperSection > Articles > Creating, Managing and Destroying Threads in C#

Creating, Managing and Destroying Threads in C#


C# C# 
Ratings:
0 Comment(s)
 3565  View(s)
Rate this:

Creating, Managing and Destroying Threads in C#

In this article I’m explaining about Creating, Managing and destroying threads in c#.

if you learn more  about Thread Synchronization read my previous post Thread Synchronization in c#

Creating Threads:

Threads are created by extending the Thread class. The extended Thread class then calls the Start () method to begin the child thread execution.

using System.Threading;

 

namespace ThreadManaging

{

    class Program

    {

        public static void ThreadB()

        {

            Console.WriteLine("Thread B Starts");

        }

        static void Main(string[] args)

        {

            ThreadStart thread = new ThreadStart(ThreadB);

            Console.WriteLine("In Main Thread: Creating the Thread B");

            Thread childThread = new Thread(thread);

            childThread.Start();

            Console.ReadKey();

        }

    }

}

 

Output

Creating, Managing and Destroying Threads in C#

 

Managing Threads:

The Thread class provides various methods for managing threads. The following example demonstrates the use of the sleep () method for making a thread pause for a specific period of time.

using System.Threading;

 

namespace ThreadManaging

{

    class Program

    {

        public static void ThreadB()

        {

            Console.WriteLine("Thread B Starts");

            int sleep = 10000;

            Console.WriteLine("Thread B Paused for {0} Seconds",sleep/1000);

            Thread.Sleep(sleep);

            Console.WriteLine("Thread B Resumes");

        }

        static void Main(string[] args)

        {

            ThreadStart thread = new ThreadStart(ThreadB);

            Console.WriteLine("In Main Thread: Creating the Thread B");

            Thread childThread = new Thread(thread);

            childThread.Start();

            Console.ReadKey();

        }

    }

}

 

Output

Creating, Managing and Destroying Threads in C#

 

Destroying Threads:

The Abort () method is used for destroying threads. The runtime aborts the thread by throwing a ThreadAbortException. This exception cannot be caught, the control is sent to the finally block, if any.

using System.Threading;

 

namespace ThreadManaging

{

    class Program

    {

        public static void ThreadB()

        {

            try

            {

                Console.WriteLine("Thread B Starts");

                for (int i = 0; i <= 10; i++)

                {

                    Thread.Sleep(500);

                    Console.WriteLine(i);

                }

                Console.WriteLine("Thread B Completed");

            }

            catch (ThreadAbortException e)

            {

                Console.WriteLine("Thread Abort Exception" + e.Message);

            }

            finally

            {

                Console.WriteLine("Could not catch the Thread Exception");

            }

 

        }

 

        static void Main(string[] args)

        {

            ThreadStart threadstart = new ThreadStart(ThreadB);

            Console.WriteLine("In Main Thread: Creating the Thread B");

            Thread thread = new Thread(threadstart);

            thread.Start();

            Thread.Sleep(2000);

            Console.WriteLine("In Main Thread: Aborting the  Thread B");

            thread.Abort();

            Console.ReadKey();

        }

    }

}

 


Output

Creating, Managing and Destroying Threads in C#

 in my next post, i'll explain about Hashtable and Dictionary in c#


Don't want to miss updates? Please click the below button!

Follow MindStick