ZDNet UK


Skip to Main Content

ZDNet.co.uk - Winner of Best Business Website 2007
  1. Home
  2. News
  3. Blogs
  4. Reviews
  5. Jobs
  6. Resources
  7. Community
  8. My ZDNet

 

ZDNet UK RSS Feeds


Application development Toolkit

Multithreading basics in .Net Framework

Stephen Fraser Builder.com

Published: 04 Feb 2003 15:14 GMT

  • Email
  • Trackback
  • Clip Link
  • Print friendly
  • Post Comment

Suspending and resuming threads
Interrupting will work fine if the worker thread knows when to go to sleep. You may also need to allow another thread to temporarily stop a different thread and then restart it again later.

For example, a worker thread could be doing some intense number crunching when along comes another thread that needs to put a large graphic up on the monitor ASAP. (User interface should almost always get priority.)

This scenario can be resolved in at least three ways. You could do nothing special and let the multithreading engine slowly display the graphic. You could raise the priority of the graphic display thread (or lower the priority of the worker thread), thus giving the graphic display more cycles. Or you could suspend the worker thread using the Suspend() method:


Visual Basic
Public Sub Suspend()

C#
public void Suspend();

C++
public: void Suspend();

Then, you could draw the graphic and resume the worker thread using the Resume() method:

Visual Basic
Public Sub Resume()

C#
public void Resume();

C++
public: void Resume();

Background and foreground threads
Two types of threads are available. The first is the foreground thread, which runs until it reaches its exit point or is aborted. The second is the background thread, which terminates upon the completion of the last foreground thread. When you create a thread using the Thread class' constructor, you create a foreground thread by default. To change the thread to a background thread, you must change its IsBackground property to true. The IsBackground property can be toggled at any time while the thread is running.

Everything in action
Enough theory; now let's see threads in action. The following example (there are three versions, in different languages) is a true mish mash of threads using all of the method discussed above.

There really isn't much rhyme or reason to this example, but all three versions generate output similar to Figure A.


Figure A

As you start to play with threads, you'll come to realise that even though they're easy to code, they're not easy to work with. You will spend a lot of time just trying to get your threads to synchronise. Hence, the inordinate number of Sleep() and Join() method calls placed in the simple example.

Remember, these are just the basics. Many other tools are available to build your .Net Framework multithreaded applications.


For a weekly round-up of the enterprise IT news, sign up for the Enterpise newsletter.

Find out what's where in the new Tech Update with our Guided Tour.

Tell us what you think in the Enterprise Mailroom.

Next

Previous

1 2 3


  • Email
  • Trackback
  • Clip Link
  • Print friendlyPrint with Konica

Did you find this article useful?
159 out of 315 people found this useful


Full Talkback thread

0 comments

Company/Topic Alerts

Create a new alert from the list below:












Discussions

Telic Telic

MacLinux

Friday 10 October 2008, 10:34 PM

5 comments
1000215420 1000215420

Punishment & Deterrent

Friday 10 October 2008, 9:37 PM

4 comments
1000215420 1000215420

Punishment & Deterrent

Friday 10 October 2008, 9:36 PM

4 comments

Featured Talkback

In association with Intel
The fact is: Software developers today are really designers and not coders. The reason that business anlaysts exist today to model solutions is because they understand the value of designing software before writing it. All too often developers create code that has little value because they do not understand that business classes interact with other classes within the confines of a working model or pattern.

By: 1000165269

Read full story:
Making sense of agile modelling