Advertisement
Promo

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 friendlyPrint with EPSON

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


Full Talkback thread

0 comments


Company/Topic Alerts

Create a new alert from the list below:












Video icon

Video

Discussions

CA CA

No one should be....

Thursday 10 December 2009, 10:38 PM

1 comment
CA CA

we'll..

Thursday 10 December 2009, 9:55 PM

2 comments
CA CA

Pleasant surprise..

Thursday 10 December 2009, 9:17 PM

2 comments
CA CA

Questions over pub Wi-Fi case remain u...

Thursday 10 December 2009, 9:05 PM

1 comment

Featured Talkback

In association with Network Liberation Movement
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


Skip Sub Navigation Links to CNET Brand Links

Help

Become part of the ZDNet community.

Newsletters