Export (0) Print
Expand All

Microsoft .NET Compact Framework Multi-threading Tips

.NET Compact Framework 1.0
 

Content Master Ltd

April 2003

Applies to:
    Microsoft® .NET Compact Framework 1.0
    Microsoft Visual Studio® .NET 2003

Summary: Learn how to use multiple threads in a Windows Forms application with the .NET Compact Framework. (4 printed pages)

Contents

Introduction
Creating a Worker Thread
Updating the User Interface from a Worker Thread
More Info

Introduction

A rich user experience is a key facet of any interactive application. Software that requires user interaction must respond as quickly as possible to the user’s activities. At the same time, an application must be able to process data and prepare it for display.

Using multiple threads within an application allows you to separate out the tasks that are performed by the user interface (UI) from the tasks that are carried out in the background. Organizing tasks in this manner allows the UI to remain responsive to user input while the background processing takes care of the data processing.

Creating a Worker Thread

An application can create one or more threads to execute a method. The first step in creating a worker (or secondary) thread is to create a ThreadStart delegate, specifying the method to be executed by the thread. The ThreadStart delegate is then passed to the constructor for the Thread class. For example, to start the new thread and execute the MyFunction method, call the Start method of the Thread class as shown below:

ThreadStart starter = new ThreadStart(MyFunction);
Thread t = new Thread(starter);
t.Start();

Updating the User Interface from a Worker Thread

You can update the user interface (UI) from a thread other than the UI thread by using Control.Invoke. This method executes a delegate in the control’s thread context on the UI thread. The .NET Compact Framework supports just one of the overloaded Control.Invoke methods from the full .NET Framework. Control.Invoke takes a single parameter; a delegate specifying which method to execute on the UI thread. The delegate must be of type EventHandler and have the following signature:

void MyFunctionName(object sender, EventArgs e)

An important point to note is that you must call Application.DoEvents() in your code if you are updating the UI in the worker thread. Calling Application.DoEvents() will make sure that any events raised by the worker thread are processed by the UI thread.

The following sample code demonstrates how to create a worker thread and then update a ListBox control called listBox1 from both the UI thread and the worker thread:

// this variable will hold some text set by the worker thread
public string Message = "";

// Create a worker thread and then add items to the ListBox from the
// UI thread
public void DoThreading()
{
    // Create the worker thread and start it
    ThreadStart starter = new ThreadStart(this.UpdateListBox);
    Thread t = new Thread(starter);
    t.Start();

    // Loop 4 times, adding a message to the ListBox each time
    for(int i = 0; i < 4; i++);
    {
        this.listBox1.Items.Add("Message from UI thread");
        this.listBox1.Update();
        // Process any queued events on the UI thread
        Application.DoEvents();
        // Suspend processing for 1 second
        Thread.Sleep(1000);
    }
    this.listBox1.Items.Add("Last message from UI thread");
    this.listBox1.Update();
}

public void UpdateListBox()
{
    for(int j = 0; j < 5; j++)
    {
        // Set the message to be added to the ListBox from the worker 
        // thread
        this.Message = "Worker thread loop count = " + j.ToString();
        // Invoke the WorkerUpdate method in the ListBox’s thread
        // context
        this.listBox1.Invoke(new EventHandler(WorkerUpdate));
        Thread.Sleep(700);
    }
}
// The delegate that’s called from the worker thread
// to update the ListBox
public void WorkerUpdate(object sender, EventArgs e)
{
    this.listBox1.Items.Add(this.Message);
    this.listBox1.Update();
}

To use this code:

  • Create a new Smart Device Application using the Windows Application template.
  • Add a ListBox control to the Windows Form (the default name will be listBox1).
  • Add the statement using System.Threading; to the list at the top of the Form1.cs file.
  • Paste the code into the Form1 class.
  • Call the DoThreading method from the Form1 constructor.

More Info

For more information, please see the resource below:

Show:
© 2014 Microsoft