Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  How to create Threads
How to create Threads
by John Spano | Published  11/20/2002 | .NET Framework | Rating:
John Spano

John Spano cofounder and CTO of NeoTekSystems, a Greenville, South Carolina technology consulting company. NeoTekSystems offers IT consulting, custom programming, web design and web hosting. We specialize in Microsoft .Net enterprise development and business design.

I have six years of experience in software architecture. My primary focus is on Microsoft technologies, and I have been involved in .NET since beta 1. I currently hold a MCSD certification, 2 MCTS's (Windows, Web) a MCPD in Distributed, 2 MCITP's, a Microsoft MVP, and have won the Helper of the Month contest for July 2002 in the devCity.NET forums.

Corporate URL: www.NeoTekSystems.com
Primary email: JSpano@NeoTekSystems.com
Alternate email: Jspano@devcity.net.

 

View all articles by John Spano...
How to create Threads

Creating threads in VS.NET is much easier than in previous Visual Studio languages. Here is an introduction on how to create a thread and have it execute a function in C#

First you must reference the thread functions

Code:

using System.Threading;

The code we want to run on our thread

Code:
protected void DoSomethingOnAThread()
{
  string s ;
  s = "This is output from another thread";
  Console.WriteLine(s);
}

To start our thread and have it execute the above function

Code:
thread = new Thread(new ThreadStart( DoSomethingOnAThread ));
thread.Start();

Now our function will be called by the new thread. When the function exits the thread will be terminated.

To set the windows priority of the thread use the following constants:
Normal, AboveNormal, BelowNormal, Highest, and Lowest

Code:
thread.Priority = ThreadPriority.Highest;

To kill a thread, see if it is really running and then call the abort method on the thread

Code:
if ( thread.IsAlive )
{
     thread.Abort();
}

You can pause a thread for a fixed length of time. If you specify 0 as the time, the thread is suspended and will let any other threads run. If you specify the Infinit constant the thread will be blocked indefinitely. The function take an integer as the milliseconds to sleep or a Timespan variable

Code:
thread.Sleep(10);

To suspend a thread use

Code:
if (thread.ThreadState = ThreadState.Running)
{
     thread.Suspend();
}

To resume a suspended thread

Code:
if (thread.ThreadState = ThreadState.Suspended)
{
     thread.Resume();
}

Threading has many benifits:
  • You can process long tasks in the background
  • You can make a user interface look better by not locking it when the user clicks something. This gives the feel of a fast well working user interface. An example of this is MS word. When you save a document you get a little icon on the status bar that shows a disk and a progress bar of the save. While it is saving you can cotinue to work
  • You could do network comunications over a different thread and have users doing other things while waiting on data

Threads also have some pitfalls:
  • If you have a large number of threads, it can hurt performance while the OS switches between them
  • The more threads you have the more memory your app needs
  • Many threads can be a big source of bugs
  • Killing threads means you have to know exactly what will happen if you kill the thread.
  • If you have variables that are accessed by more than one thread you have to make sure you have a good variable locking plan

This article was originally posted as devCity.NET Forums FAQ - http://www.devcity.net/forums/faq.asp?fid=31#TID4902

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.57142857142857 out of 5
 14 people have rated this page
Article Score8838
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 01/24/2005)
Rating
I have no idea about threading issues , and this article was very clear for me :)
thanks !
 
Comment #2  (Posted by an unknown user on 04/06/2005)
Rating
All sample code I find about threading in the .NET enviroment is written in C#. I word my searches for Visual Basic.net, so C# articles are useless to me.
 
Comment #3  (Posted by an unknown user on 08/23/2005)
Rating
Outstanding

 
Comment #4  (Posted by an unknown user on 03/07/2006)
Rating
very good
ks
 
Comment #5  (Posted by an unknown user on 06/21/2006)
Rating
Concise, excellent. I use VB.NET, but I'm able to translate the concept from the C# example.
 
Comment #6  (Posted by an unknown user on 04/19/2007)
Rating
becuz u got it all set up and know wut ur doing
 
Comment #7  (Posted by an unknown user on 05/21/2008)
Rating
I give following error thread is a namespace but use as a type
 
Comment #8  (Posted by an unknown user on 05/29/2008)
Rating
Thanks for providing the solution coz sometimes this timer control sucks
 
Comment #9  (Posted by an unknown user on 07/07/2008)
Rating
Thanks for the help...
 
Sponsored Links