Thread basics using followings:
- How to Create thread; use System.Thread() class and create an instance
- How to Join thread; use object.Join() to join threads.
- How to Suspend thread; use object.Sleep(<No of seconds>) to Suspend thread
- How to Kill thread; use object.Abort() to Suspend thread
- Using Interlocked class which uses Increment and decrement method to increment and decrement values.
- Using Locks: allows to mark a lock on the critical section of the code(program), provides synchronization to an object and then to execute the lock is in effect.
- Using Monitor: allows to decide when to enter and exit the synchronization, and it lets us wait for another area of code to become free. Monitor acts as a smart lock on a resource. When we need synchronization, we can call the enter() method of the monitor passing the object we want to monitor. We can explicitly choose to wait () method of the monitor to control thread ordering. In case of using wait () waiting threads will be allowed to notify of a chance to run again if the active thread calls Pulse (). This signals the CLR that there has been a chance in the state that might free a thread that is waiting. The CLR tracks of the fact that the earlier thread asked to wait and the thread will be guaranteed access in the order in which the wait where requested. Exit () can be called once when the thread is finished with the monitor.

Architecture of CLR
Threading support is inbuilt under Common Language Runtime provided by Microsoft .NET Framework.
Understanding Threading
- Threads are basically lightweight processes responsible for multitasking within a single application.
- The base class used for threading is System.Threading
- Threads are managed under the Common Language Runtime, programmers don't have to manage any threads explicitly.
- Using threading with the combination of components it is recommended to use explicit definition and management of the thread.
- Threads are implemented when you have situations in which you want to perform more than one task at a time.
- In case of Synchronization. Since you have limited amount of recourses, you may have to restrict the access to the resource to one thread at a time. In this situations, you may implement locking on the threading to overcome the scenarios.
- An apartment is a logical container within a process and is used for objects that shares the same thread-access requirement. Object in the apartment can all receive method call from any object in any thread in the apartment. And managed object (object created within CLR) are responsible for thread safety.
- Threads can be used under the situation where we must wait for an event such as user input, a read from file, or receipt of data over the network. Freeing the memory to turn it a safe, and it makes our program to appear to run more quickly.
Working with Thread
Create new instance of Thread object. Thread constructor accepts one parameters that is delegate.
MS CLR provides ThreadStart delegate class for starting the thread.
Example:
- Thread myThread = new Thread( new ThreadStart(myFunc) );
- Thread t1 = new Thread( new ThreadStart(Incrementer) );
t1.Start()
Detailed example is as below
- namespace Programming_CSharp
- {
- using System;
- using System.Threading;
- class Tester
- {
- static void Main()
- {
- // make an instance of this class
- Tester t = new Tester();
- // run outside static Main
- t.DoTest();
- }
- public void DoTest()
- {
- // create a thread for the Incrementer
- // pass in a ThreadStart delegate
- // with the address of Incrementer
- Thread t1 = new Thread(new ThreadStart(Incrementer));
- // create a thread for the Decrementer
- // pass in a ThreadStart delegate
- // with the address of Decrementer
- Thread t2 = new Thread(new ThreadStart(Decrementer));
- // start the threads
- t1.Start();
- t2.Start();
- }
- // demo function, counts up to 1K
- public void Incrementer()
- {
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine("Incrementer: {0}", i);
- }
- }
- // demo function, counts down from 1k
- public void Decrementer()
- {
- for (int i = 10; i >= 0; i--)
- {
- Console.WriteLine("Decrementer: {0}", i);
- }
- }
- }
- }
Output:

Joining Threads
Once thread starts running and in a situation when we need to tell thread to stop processing and wait until a second thread to complete the processing we need to join the 1st thread to the 2nd thread. Use following for the same. This will join the second thread to the 1st.
Example:
t2.Join( )
Suspending Thread
In a situation we some time needs to suspend running thread.
Example:
t2.Sleep(<No of Seconds>)
Killing Thread
Threads have 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.
Example:
t2.Abort()
Advanced Threading
Synchronization
Recollect the discussion we had before in some situations we need to synchronize the running threads, so we can modify the running thread and its resources.
Join the conversation! Your thoughts help the community grow.