2013-09-15

C# supports the concept of Multithreading which helps to execute two or more  "parts" of a program currently.Every part is known as a thread.A thread is a separate sequence of instructions for  executing a specific task in the program. Multithreading means,performing multiple tasks at the same time during the execution of a program.Every c# program starts the execution with a single thread which is called main thread .The Main thread is runned by the (CLR) & operating system.
          The process of developing a program for execution with multiple threads , is called multithreading programming and the process of  execution is called multithreading.
There are some advantage of threading in c#.

Optimize the use of computer resources such as memory i/o device.

Minimize the time

The Namespace for multithreading is 'System.Threading' in c#,that containing classes and interfaces that are required for developing and running multithreaded application.We can use the methods and properties contained in these classes to perform task such as synchronising the activities of a thread and creating a thread.

You can easily make any Online Applications with the help of Multithreading and Database.

There are some important classes in the 'system.threading Namespace.

Thread

ThreadPool

Monitor

Mutex

 1. ) Thread Class
  The Thread class is used to perform tasks such as creating and setting the priority a thread.We can use this class to obtained its status and a thread.
There are some important properties of thread class which are given below:

Name:-It is used to specify a name for a thread.

Priority:-It obtain a value which indicate the scheduling the scheduling priority of a thread.

Normal:- This value must be scheduled after the threads with Above Normal priority but before the Below Normal priority threads.

Highest:-The Highest value indicates that thread must be scheduled for running before any other threads.

Above Normal:-The Above Normal value indicates that the thread with this value must be scheduled after highest priority threads but before the Normal Priority thread.

Below Normal:-This value indicates that the thread with this value must be scheduled after a Normal Priority value thread but before the Lowest priority thread.

Lowest:- This value indicates that the thread with  this value must be scheduled for running after the threads with other priorities such as Normal and Highest.

Thread State:-By default ,the value of the thread state properties is unstarted.

There are some,Thread state value which are given below:

Running:- This Value indicates that the current thread is running.

Stopped:-This value indicates that the current thread is stopped.

Suspended:- This value indicates that the current thread is suspended.

Unstarted:- This value indicates that the thread has not been started yet using the start method of the thread class.

Waitsleepjoin:-The value indicates that the current thread is blocked because of call to wait,sleep or join method.

IsThreadpoolthread:-This value indicates whether a thread is part of a thread pool or not. The value of IsThreadPoolThread properties is true if the thread is part of a thread pool,otherwise,it is false.

To retrieve the name of the thread,which is currently running.

IsAlive:- This value indicate the current state of thread execution . The value of IsAlive property is true if the thread has been started otherwise it is false.

There are some methods of the thread class also which provides certain methods that can be used to manage operation such as starting  thread and resuming a suspended thread which is given below:

start:-It is used for starting a thread.

Suspend:-It is used ,for suspending a thread.

Join:- Block a thread until another thread has terminated.

Resume:-It is  used for resuming a thread which has been suspended earlier.

Interrupt:-It is used to interrupt the thread,which is in the waitsleepjoin state.

SpinWait:- It is used to make  a thread wait the number of times specified in Iteration parameter.

Abort:-It is used to terminate the thread as soon as possible.

Interrupt:-It is used to interrupts the current thread from a suitable wait period.

2. ) ThreadPool class:-
This class provides a pool of threads that helps to perform tasks such as processing of asynchronous i/o and waiting on behalf of another thread. 
There are some important methods of ThreadPool class which are given below:

GetType:- It is used to obtain the type for the current thread pool.

Equals:- It is used to determine whether two thread pool are equal or not.

SetMaxThreads:-It is used ,to specify the number of requests to the ThreadPool that can be concurrently active.

SetMinThreads:-It is used to specify the number of idle threads that can be maintained by a thread pool for new requests.

QueueuserWorkItem:-It allows a method to be queued for execution.

3. ) Monitor class:-
This class provides control access to an object by granting a lock for the object to a single thread.When an object is locked for a thread then the access to a specific program code is restricted.
There are some important methods that allow to perform task such as acquiring and releasing a lock for an object,which are given below:

GetType:- It is used to allow to obtain the type for the current instance of the monitor class.

Enter:-It is used to allow a thread to obtain a lock on a specified object.

TryEnter:-It is used to allow a thread to try and obtain a lock on a specified object.

Wait:-It is used to allow  thread to release the lock on a object and block the thread for the time period until which it again obtains the lock.

Exit:-It is used to allow a thread to release a lock on  specified object.

4. ) Mutex class:
A mutex is basically used for synchronistion.It helps to perform interprocess synchronisation in c#.Mutex allows a thread to have exclusive access to shared resources when a thread to have exclusive access to shared resources .When a thread obtains a mutex,another thread,which wants to obtained the mutex,is suspended until the first thread relese the mutex.
There are some important methods in mutex class which are given below:

Equals:-It is used ,to determine whether two mutex are equal are not.

SetAccessControl:- It is used, to set the access control security for a specified mutex.

ReleaseMutex:-It is used to release a mutex once.

OpenExisting:- It is used to open an existing mutex.

cl0se:- It is used to release the resources held by an object of the wait handle class.

Note:-The mutex class provides the handle and safe wait handle properties that can be used to retrieve the handle for the operating system.

Examples:
1. ) Creating and starting the threads:-

output:-



2. ) Creating a thread with timer:-

Output:-



scheduling a Thread:-
The threads can be scheduled by setting the priority of the thread using the priority property of the thread class.There are some priority of thread class which are given below:

Highest

AboveNormal

Normal

Below Normal

Lowest

Example:-

output:-



Synchronising Threads:-
The Thread can be synchronised by using methods such as sleep and join of the thread class.
Example of ThreadPool:-

Output:-

Example of Lock keyword:-

Output:-

Example of Mutex:-

Output:-

Description:- In above example ,all three thread follow the mutual exclusion.Means at one time only one thread will enter the critical section(safed area),which is shown in above example.

Types of Thread:-

Foreground thread

Background thread

Example of sequential execution of program:-

Output:-

Description:-
In above example ,first function 1 runs and after that function 2 runs.It is known as synchronous processing or sequential processing.Now in Next program,I will show you how to implement  threading in this program using c#.In Multithreading each function(thread) runs parally.In other word parallel execution of program is known as multithreading.

Example of Parallel Execution of program:-
Ex.

Output:

Description:-Above program executes the parally manner.In this computer processor gives the time slot to execute the each program.
1.) Foreground Thread:-
Foreground thread are those thread even main application is exited but it continue running whenever foreground thread is not stopped.

Output:-

Description:-
In above example if we run the program then it show that "The main application has exited" but one thread is running which is known as foreground thread. after putting the input ,it shows "function1 is exited".it is follow the definition of foreground thread.

2.) Background Thread:-
A Background thread is die off if the main application is finished.
Ex.

Output:-

Description:-
In above example ,if we run the application,its showing"The main application has exited".if main thread is exited then background thread is also exited.Which is shown in above examples.
Note:-For above example you can cover many things like:

No need for c sharp training.

Use of Database in Threading.

Make Online Application.

Scheduling

For More:-

Send mail from aps.net application free

Create setup file with database

Binding Navigator control

File handling Real Application

Directory and Directory info class

.Net Interview Questions & Answers

To Get the Latest  Free Updates Subscribe
Download whole Application from Below:-
                 Download

Show more