Home > Articles > Programming > .NET and Windows Programming

Multithreading with the .NET Framework

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close Window

Peter Aitken

Learn more…

Debugging 101 for Visual Studio
Sep 15, 2006
Fun with Fractals in Visual Studio .NET
Aug 11, 2006
Exploring Excel Functions, Part 9: Analyzing Frequency Distributions
Aug 4, 2006
File Management in .NET
Jul 7, 2006
Exploring Excel Functions 8: Predicting the Future
Jun 30, 2006
Exploring Excel's Functions, Part 7: Rounding with Excel
Jun 23, 2006
Binary File Access in the .NET Framework
Jun 16, 2006
Text File Access in the .NET Framework
Jun 2, 2006
Using Geometric Transforms for Text Effects in .NET
May 26, 2006
Understanding MDI Applications in .NET
May 12, 2006
A .NET Framework Text and Font Primer
Apr 21, 2006
Exploring Excel's Functions, Part 6: TTEST() Function
Apr 14, 2006
Troubleshooting Excel PivotTables
Apr 7, 2006
Troubleshooting Word Tables
Mar 31, 2006
Exploring Excel's Functions, Part 5: The Power of Choice
Mar 24, 2006
Seven Things I Hate About Word Printing
Mar 10, 2006
Exploring Excel's Functions, Part 4: Database Functions
Feb 24, 2006
Exploring Excel's Functions Part 3: CELL() Shocked
Feb 3, 2006
Exploring Excel's Functions Part 2: ADDRESS() and INDIRECT()
Dec 30, 2005
Exploring Excel's Functions: IF() Only!
Dec 9, 2005
Advanced Find-and-Replace Tools in Word
Oct 28, 2005
Going Beyond Basic Spaces and Hyphens in Word
Sep 16, 2005
Get Organized with Word's Outline Tools
Aug 19, 2005
Managing Your Money in Microsoft Excel: Basic Financial Calculations
Jul 29, 2005
Structured Exception Handling in Visual Studio .NET
Oct 8, 2004
Multithreading with the .NET Framework
Jun 11, 2004
.NET Tools for Working with XML
May 21, 2004
Storing Information: Variables and Constants in C
Mar 28, 2003
Introducing Web Programming with .NET
Mar 1, 2002
Using Web Forms
Mar 1, 2002
Introducing Web Services
Feb 15, 2002
XML and the .NET Framework
Feb 8, 2002
ASP.NET Programming: Using Web Forms
Jan 25, 2002
Understanding the Common Features of Web Controls
Aug 20, 2001

Sorry, this author hasn't posted any blogs.

Every programmer needs to know how and when to use multitasking. Peter Aitken shows you how easy it is to accomplish in .NET.
As I type this article into my computer, it is busy with a couple of other tasks—printing a photo on my inkjet and downloading some trial software from the Internet. It's cool, not to mention useful, to do three things at the same time! However, this apparent multitasking is just an illusion, because any single-CPU computer is limited to doing one thing at a time. This magic trick is pulled off using threads, and an understanding of how threads work and how to use them in your .Net programs belongs in every programmer's skill set.

Thread Basics

When your computer appears to be doing multiple things at once, what is really happening is that the CPU's time is being divided up between the various tasks. Task 1 — printing, for example — gets the CPU for some fraction of a second, then task 2, task 3, and so on. Because the computer is very fast and human perceptions are relatively slow it seems as if the tasks are being done simultaneously. This ability to share the CPU among multiple tasks is called multitasking, and it is a feature of the Windows operating system (and most other operating systems too).

The thread is the basic unit of multitasking, the entity to which the operating system assigns CPU time. Each program typically has a single thread, as do various background processes associated with the operating system. The operating system assigns CPU time to the running threads based on their priority. In Windows, a thread can have one of four priorities as described here from highest to lowest:

  • Real time: used for processes that cannot be interrupted, such as games with complex graphics and streaming video.
  • High: used for time-critical processes that must execute essentially immediately for proper function. The Windows Task manager is an example.
  • Normal: used for processes that have no special CPU scheduling demands. Most application programs, such as a word processor or spreadsheet, are assigned this priority.
  • Idle: used for processes that should run only when no higher priority process is active, in other words when the system is idle.

How does threading relate to the .Net programmer? A .Net program by default has a single thread, but you can create two or more threads within a single program if you desire. Because these threads are all part of the same program, or process, they all have access to the program's resources (global, static, and instance fields, for example). But because Windows allocates CPU time based on threads, not processes, a multithreaded program will get more than its normal share of CPU time.

What exactly does this mean in practical terms — does the program run faster? Generally speaking, no, at least on single processor computers. (Multiple processor systems are another story and beyond the scope of this article). It means, rather, that the program (or some programs, anyway) can be more responsive to the user. When a program has only a single thread, that thread is responsible for all program operations, including responding to user input, such as typing text into a text box, selecting menu commands, and clicking buttons. If the thread is occupied with long-running data processing, such as mathematical calculations or XSLT processing, response to the user can become sluggish. Click a menu title, for example, and it may take several seconds for the menu to appear. This can be very frustrating to the user, and a competent programmer will see that it never happens. By creating a second thread and assigning the long-running process to it, the programs initial, default thread can respond immediately to user input.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on November 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network