How to Create, Stop and Suspend Threads in C#

Nov 15, 2002 - © Jose Aniceto

The Windows operating system can run multiple tasks or applications at the same time.

The term Thread in .Net and Windows means a series of commands executed in sequence. For example, an application is considered single threaded when the application is started, does some processing and then terminates. An application would be considered multi-threaded when the application branches off, creating two streams of execution.

Multi-threaded applications are useful in writing a server-type application, like a web server. On high volume web sites, as soon as a client requests a page, the web server creates a thread to service this request and then as the newly created thread processes the request, the main thread waits for the next connection request.

ThreadSample.cs
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Threading;

namespace Suite101.CSharp
{
   public class ExampleThreads
   {
      public static void Main()
      {
         ExampleThreads sample = new ExampleThreads();
         sample.CreateThreads();
      }

      public void CreateThreads()
      {
         Thread firstThread = new Thread( new ThreadStart( FirstEntryPoint ) );
         Thread secondThread = new Thread( new ThreadStart( SecondEntryPoint ) );

         firstThread.Start();
         secontThread.Start();
      }

      public void FirstEntryPoint()
      {
         // Do Nothing
      }

      public void SecondEntryPoint()
      {
         // Do Nothing
      }
   }
}

Creating a Thread

C# has support to create threads through .Net's base class library. The Thread class also has other helper methods to help manage and control threads. The sample code above is an example of how to create two threads. To create a thread, you would have to create two classes, Thread and ThreadStart.

Tell ThreadStart the entry point of the thread. In our example, we are creating two threads with two separate and distinct entry points. Once the thread class is instantiated, threads do not start until the Start() method is called.

Stopping a Thread

Normally, when a thread is started, it runs until  finished. However, it is possible to stop a thread by calling the Abort() method. In our example, if we want to stop firstThread, you would add the following code.

firstThread.Abort()

Due to the built in garbage collection of .Net, calling the Abort() method does not immediately terminate a thread.

Suspending or Interrupting a Thread

It is possible to suspend the execution of a thread by calling the Suspend() method. Suspending a thread allows other applications to access system resources. If your thread requires some computing power, suspending it from time to time allows other applications to run.

The Suspend() method takes sleeping period as a parameter, in milliseconds. By passing a parameter, a thread is suspended for that period. Alternatively, the Sleep method can also be used to suspend thread execution.

firstThread.Suspend( 5000 )

If you have a thread that is already in suspend or sleep mode, you can wake it up by calling the Interrupt() method.

firstThread.Interrupt()

Creating threads is not easy, but is a good strategy to employ to maximize computer resources.

The copyright of the article How to Create, Stop and Suspend Threads in C# in C# Programming is owned by Jose Aniceto. Permission to republish How to Create, Stop and Suspend Threads in C# in print or online must be granted by the author in writing.


Articles in this Topic