Threading enables your C# program to perform concurrent processing so you can do more than one operation at a time.
C# supports parallel execution of code through multithreading.Threads are relatively
lightweight processes responsible for multitasking within a single
application.Thread is an independent execution path, able to run simultaneously
with other threads.
For implementing Threading, you first include System.Threading namespace in your project.
class ThreadClass
{
static void Main() {
Thread thread1 = new Thread (Display("1");
Thread thread2 = new Thread (Display("2"));
thread1.Start();
thread2.Start(); // Run on the new thread
}
static void Diaplay(string strparameter)
{
while (true) Console.Write(strparameter); // Write forever
}
}
In the above program, two thread simultaneously running.
We can share a data between multiple thread through static variable.
class ThreadClass {
static void Main() {
static int num;
Thread thread1 = new Thread (Display("Thread 1");
Thread thread2 = new Thread (Display("Thread 2"));
thread1.Start();
thread2.Start(); // Run on the new thread
}
static void Diaplay(string strparameter) {
while (true)
{
num ++;
Console.Write (num.ToString() + " " + strparameter); // Write forever
}
}
}
In the above program, we are declaring a num static variable and this number is incrementing
by both thread.
Joining Threads:-Joining is used for blocks the calling thread until a thread terminates. Means
thread to stop processing and wait until a second thread completes its work,
----------
Once a thread starts running and in some situation if we need to tell the thread to stop processing and wait until a second thread completes processing, we need to join the first thread to the second thread. Use the following for the same. This will join the second thread to the first one.
---------
Sleep Threads:- you want to suspend your thread for a short time. For this Thread
class provide a public static method "Sleep". Sleep is an overloaded function.
Thread.Sleep (Int32): Suspends the current thread for a specified time.
e.g. thread1.Sleep(1000) //Sleep for 1000 millisecond
Thread.Sleep (0); // give up CPU time-slice
Thread.Sleep (TimeSpan): Blocks the current thread for a specified time.
e.g. Thread.Sleep (TimeSpan.FromSeconds (5));
Thread.Sleep (TimeSpan.FromHours (1)); // sleep for 1 hour
Thread.Sleep (Timeout.Infinite); // sleep until interrupted
Killing Threads: Thread kill itself after completing its task, but you want to kill thread
before completing its task. For this you can use Abort() function.
e.g. thread1.Abort();
-----------
Threads has to die after the execution of the process in normal situations, occasionally it is required for the programmer to kill a thread. Threads can be killed using the following:
-----------
Suspend Thread: You want to stop executing your thread then you use Suspend() method and for running the suspending thread
You use Resume() method.
Thread Priority: Thread priority is basically used for giving which thread is high priority or
low priority. There are five level of priority
*Highest
*AboveNormal
*Normal
*BelowNormal
*Lowest
e.g.
thread1.Priority = ThreadPriority.BelowNormal;
thread2.Priority = ThreadPriority.Highest;
Thread.ThreadState Property: ThreadState property gives the current state of thread status.
The initial value is Unstarted.
ThreadState.Aborted
ThreadState.AbortRequested
ThreadState.Stopped
ThreadState.Unstarted
ThreadState.WaitSleepJoin;
ThreadState.Background;
ThreadState.SuspendRequested;
ThreadState.Suspended
All of the above property return true or false on the basis of its status.
if you want to check abort status of thread1 then
if ( thread1.ThreadState == ThreadState.Suspended}
{
thread1.Resume();//to again start the thread
}
Rsume method is used for running the suspended thread. if the thread is not suspended then there will be no effect on thread status.
LOCK
====
"Change Style(Copied)
lock marks a critical section of the code, and provides synchronization to an object.
Example:
public void Incrementer( )
{
try
{
while (counter < 1000)
{
lock (this)
{
int temp = counter;
temp ++;
Thread.Sleep(1);
counter = temp;
}
// assign the decremented value
// and display the results
Console.WriteLine("Thread {0}. Incrementer: {1}",
Thread.CurrentThread.Name, counter);
}
}