Saturday, December 4, 2010

Understanding the Main thread in Console application.

The main thread is created automatically on the start up of a C# program execution. The threads which are created exclusively using the Thread class are called as child threads, where the main thread is either called a parent thread or a primary thread. You can access a thread using the CurrentThread property of the Thread class.

The following example shows the execution of main thread using its CurrentThread property in the Thread class.

using System;
using System.Collections.Generic;

using System.Threading;
using System.Text;

namespace ThreadExample
    class MainThreadExample
        public static void Main(string[] args)
            Thread Th = Thread.CurrentThread;
            Th.Name = "MainThread";
            Console.WriteLine("The current thread after name change:{}", Th.Name);


Output is display:
The current thread after name change: Mainthread

Note: -
A reference to the current thread is obtained by using the CurrentThread parameter of the Thread class and its reference is stored in the Th variable. The Name parameter is used to set the name of the thread, and the information about the thread is displayed.

No comments:

Recent Posts