ZDNet UK


Skip to Main Content

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

 

ZDNet UK RSS Feeds


IT Jobs

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

In fact, almost any programmer can delve into it and master the fundamentals. I'll introduce you to the basics of multithreaded programming and provide some sample code you can use to start experimenting.

Creating threads
To get multithreaded programming running, you must first create an instance of a Thread. You don't have much in the way of options, as there is only one constructor:

Visual Basic
Public Sub New( ByVal start As ThreadStart )

C#
public Thread( ThreadStart start );

C++
public: Thread(ThreadStart* start );

The parameter ThreadStart is a delegate to the method that is the starting point of the thread. The signature of the delegate is a method with no parameters and returns nothing, as demonstrated below:

Visual Basic
Public Delegate Sub ThreadStart()

C#
public delegate void ThreadStart();

C++
public __gc __delegate void ThreadStart();

Starting threads
One thing that may not be obvious when you first start working with threads is that creating an instance of the Thread object does not cause the thread to start. To get the thread to start, you need to call the Thread class's Start() method, like this:

Visual Basic
Public Sub Start()

C#
public void Start();

C++
public: void Start();

Getting a thread to sleep
When developing a thread, you may find that you don't need it to continually run, or you might want to delay the thread while some other thread runs. To handle this, you could place a delay loop like a "do-nothing" for-loop. But that would waste CPU cycles. Instead, you should temporarily stop the thread or put it to sleep.

Doing this couldn't be easier. Simply add the following static/shared Sleep() method:

Visual Basic
Overloads Public Shared Sub Sleep(Integer)

C#
public static void Sleep(int);

C++
public: static void Sleep(Int32);

This line causes the current thread to go to sleep for the interval specified either in an integer value of milliseconds, as the examples above show, or using a TimeSpan structure.

The Sleep() method also takes two special values: Infinite, which means sleep forever, and 0, meaning give up the rest of the thread's current CPU time slice.

A neat thing to remember is that the main function is also a thread. This means you can use Sleep() to make any application sleep.

Next

Previous

1 2 3


  • Email
  • Trackback
  • Clip Link
  • Print friendly Print with Dell

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:












Related Jobs

Web Analyst Programmer

Worcestershire County Council provides an attractive environment where work / life balance for our people is a key priority and where we are keen to ...

Application Management Analyst - Java

There may also be opportunities of periods of secondment to Accenture development teams and client sites as required - Requirement to be a member of ...

Projett Manager c.35k Banking Business Change East Midlands

The main accountabilities are: -Operating in accordance with the requirements of the Strategic Change Execution Board Standard, the Group Change ...

Discussions

AdamW AdamW

Linux, Laptops and Dual Displays

Saturday 26 July 2008, 6:34 PM

2 comments
keithmv keithmv

Password Deadlock

Saturday 26 July 2008, 12:02 PM

2 comments

Featured Talkback

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