October 28, 2016
Hot Topics:

Sample Chapter: The .NET Base Class Libraries

  • January 27, 2004
  • By Kate Gregory
  • Send Email »
  • More Articles »

The System::Threading Namespace

Threading has been a difficult part of Windows programming from the very beginning. It's quite a bit simpler in .NET. The vital classes for threading are in the System::Threading namespace. These include classes such as Mutex, Thread, and ThreadPool, which developers with experience in threaded applications will recognize instantly.

A thread is a path of execution through a program. In a multithreaded program, each thread has its own stack and operates independently of other threads running within the same program.

How Many Threads? - Any application always has at least one thread, which is the program's primary or main thread. You can start and stop as many additional threads as you need, but the main thread keeps running as long as the application is active.

A thread is the smallest unit of execution, much smaller than a process. Generally each running application on your system is a process. If you start the same application twice (for example, Notepad), there are two processes: one for each instance. It is possible for several instances of an application to share a single process: For example, if you choose File, New Window in Internet Explorer, two applications appear on your taskbar, and they share a process. The unfortunate consequence is that if one instance crashes, they all do.

Writing a multithreaded application is simple with classes from the System::Thread namespace. First, you need to think of some work for a new thread to do: some slow calculation that you want to run without slowing the responsiveness of your application. This work will go into a function, and the function will be executed on a new thread.

The Thread Proc - For a long time, Windows C++ programmers have called the function the thread proc (short for procedure).

Your thread proc must be a member function of a garbage-collected class. For example

public __gc class Counter
  void Countdown()
   String* threadName = Thread::CurrentThread->Name;
   Console::Write(S"This is the current thread: ");
   for (int counter = maxCount; counter >= 1; counter--)
     Console::WriteLine(S"{0} is currently on {1}.",threadName,
   Console::WriteLine(S"{0} has finished counting down from {1}.",

The heart of this function is the for loop that counts down from maxCount to zero. It uses the Name property of the thread that is executing the function, because the sample that uses this class calls the function on two different threads. Normally, the function that does the asynchronous work would not communicate with the user; it would be quietly working in the background, doing some long slow work. It might, however, update a progress bar, or an icon that indicates a background task is in progress.

This code calls the thread proc on a new thread and also on the application's main thread:

Console::Write("Enter a number to count down from: ");
maxCount = Int16::Parse(Console::ReadLine());
Thread::CurrentThread->Name = "Main Thread";

Counter* counter = new Counter();
ThreadStart* SecondStart = new ThreadStart(counter,Counter::Countdown);
Thread* secondThread = new Thread(SecondStart);
secondThread->Name = "Secondary Thread";


This code keeps the number entered by the user in a global variable called maxCount. Because ReadLine returns a pointer to a System::String instance, the static Parse() method of Int16 is used to convert a string to an integer so that it can be stored in maxCount.

To execute a particular function on a new thread, you create a Thread object and call its Start() method. To create a Thread object, you need a ThreadStart object, and the constructor for ThreadStart needs a reference to an instance of a garbage-collected class (counter in this example), and a function pointer (just the name of the function without the parentheses) to a member function of that garbage-collected class.

Because this code calls Countdown on a new thread and also on the main thread, the output shows the two threads taking turns, as in Figure 3.1.

Figure 3.1
A multithreaded application demonstrates how threads take turns doing their work.

In Brief

  • The libraries that come with the .NET Framework are large, and cover almost every task programmers are likely to tackle. Using them can save programmers hours or days of work.

  • The same class libraries are used in Visual Basic, C#, and managed C++, but managed C++ can also call some unmanaged libraries that Visual Basic and C# cannot access.

  • The System namespace holds classes for simple common tasks, and a large number of sub-namespaces for slightly less common (but still important) tasks, including string manipulation, IO, and threading.

Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0-672-32600-0
By Kate Gregory
Reprinted with Permission

Page 5 of 5

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel