|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article will give you basic idea about how to create a Thread in C# and how to work on the created Thread. Introduction to Threads In C#Threads: Threads are often called lightweight processes. However they are not process.es A Thread is a small set of executable instructions, which can be used to isolate a task from a process. Multiple threads are efficient way to obtain parallelism of hardware and give interactive user interaction to your applications. C# Thread:. . Net Framework has thread-associated classes in System.Threading namespace. The following steps demonstrate how to create a thread in C#. Step 1. Create a Creating an object to System.Threading.Thread creates a managed thread in .Net environment. The Thread class has only one constructor, which takes a Step 2: Create the call back function This method will be a starting point for our new thread. It may be an instance function of a class or a static function. Incase of instance function, we should create an object of the class, before we create the Step 3: Starting the Thread. We can start the newly created thread using the Thread’s For Example:
// This is the Call back function for thread.
Public static void MyCallbackFunction()
{
while (true)
{
System.Console.WriteLine(“ Hey!, My Thread Function Running”);
………
}
}
public static void Main(String []args)
{
// Create an object for Thread
Thread MyThread = new Thread(new ThreadStart
(MyCallbackFunction));
MyThread.Start()
……
}
Killing a Thread:We can kill a thread by calling the Abort method of the thread. Calling the Abort method causes the current thread to exit by throwing the ThreadAbortException.MyThread.Abort(); Suspend and Resuming Thread: We can suspend the execution of a thread and once again start its execution from another thread using the Thread object’s MyThread.Suspend() // causes suspend the Thread Execution. MyThread.Resume() // causes the suspended Thread to resume its execution. Thread State: A Thread can be in one the following state.
We can check the current state of a thread using the Thread’s Thread Priorty: The Thread class’s By Mohamed Hasan S.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||