Suite101

How to Create, Stop and Suspend Threads in C#


© 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.

Go To Page: 1 2


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

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo