Threading
Thread is independent stream in a program. Generally dot net program starts with the first statement in the main() method and continue running until reaches to return statement. Sometimes we need to run several task to run simultoneously. Because, Sometimes we have to wait a large amount of time to run some small task for a large task to be completed. This can be optimized by using threading. We can implement Threading by using System.Threading namespace.
Manipulating Threads
We can easily manipulate threads in a program by using System.Threading namespace.
I will discuss this by the following c# console application
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadManipulate
{
class Program
{
static void Main(string[] args)
{
Thread mainThread = Thread.CurrentThread;
mainThread.Name = "Main Thread";
ThreadStart customThreadStart = new ThreadStart(show);
Thread customThread = new Thread(customThreadStart);
customThread.Name = "Custom Thread";
customThread.Start();
show();
Console.ReadLine();
}
private static void show()
{
for (int i = 1; i <>
{
Console.WriteLine("This is "+ Thread.CurrentThread.Name);
Thread.Sleep(100);
}
}
}
}
Manipulating Threads
We can easily manipulate threads in a program by using System.Threading namespace.
I will discuss this by the following c# console application
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadManipulate
{
class Program
{
static void Main(string[] args)
{
Thread mainThread = Thread.CurrentThread;
mainThread.Name = "Main Thread";
ThreadStart customThreadStart = new ThreadStart(show);
Thread customThread = new Thread(customThreadStart);
customThread.Name = "Custom Thread";
customThread.Start();
show();
Console.ReadLine();
}
private static void show()
{
for (int i = 1; i <>
{
Console.WriteLine("This is "+ Thread.CurrentThread.Name);
Thread.Sleep(100);
}
}
}
}
First we have to include the System.Threading namespace. This namespace will provide us the functionality needed for Threading.
Then I manipulate the current thread (main thread) by the
Thread mainThread = Thread.CurrentThread;
statement. CurrentThread is a static readonly property of class Thread that returns the current running thread. I assign the current thread to the mainThread variable.
I also give the main thread a name "Main Thread"
mainThread.Name = "Main Thread";
Then I have created and run a custom thread by the statement
ThreadStart customThreadStart = new ThreadStart(show);
Thread customThread = new Thread(customThreadStart);
customThread.Name = "Custom Thread";
customThread.Start();
ThreadStart is a delegate that represents a method that run on System.Threading.Thread.
Then created a thread named customThread, I also give it a name "Custom Thread".
The show() method is the method that is accessed by both the thread.
when run this program output result is
This is Custom thread
This is Main thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Custom thread
This is Main thread
The output shows that two threads are running simultoneously.
So this will be very useful in case of large resources.
Control thread
Thread can be suspended by using the statement
customThread .Suspend();
Can be resumed by
customThread .Resume();
Can be aborted by
customThread .Abort();
Thread Priority
Sometimes you may want to give more processing time to an important thread than the other less important thread. You can set Priority for a thread uising ThreadPriority enumeration.
The Threadpriority enumeration contains five values
Lowest The Thread can be scheduled after threads with any other priority.
BelowNormal The Thread can be scheduled after threads with Normal priority and before those with Lowest priority.
Normal The Thread can be scheduled after threads with AboveNormal priority and before those with BelowNormal priority. Threads have Normal priority by default.
AboveNormal The Thread can be scheduled after threads with Highest priority and before those with Normal priority.
HighestBelowNormal The Thread can be scheduled after threads with Normal priority and before those with Lowest priority.
Normal The Thread can be scheduled after threads with AboveNormal priority and before those with BelowNormal priority. Threads have Normal priority by default.
AboveNormal The Thread can be scheduled after threads with Highest priority and before those with Normal priority.
You can set these values like this
customThread.Priority = ThreadPriority.AboveNormal;
Synchronization
Sometimes shared variables may be accessed by more than one thread, this will result unexpected problem.
Fortunately C# provide easy way to maintain syncronization. C# provide a keyword Lock for the purpose
Lock(x)
{
Dosomething();
}
What the lock statement does is wrap an object known as a mutual exclusion lock, or mutex, around the variable in the round brackets. The mutex will remain in place while the compound statement attached to the lock keyword is executed. While the mutex is wrapped around a variable, no other thread is permitted access to that variable. You can see this with the preceding code; the compound statement will execute, and eventually this thread will lose its time slice. If the next thread to gain the time slice attempts to access the variable x, access to the variable will be denied. Instead, Windows will simply put the thread to sleep until the mutex has been released.
Synchronization should only be used where it is necessary.
1 comment:
Welcome to blog world Mizan vi :)
You started to write about technical things which will be handy to all of us :)
Post a Comment