.NET Micro Framework Wiki :: Threads

From .NET Micro Framework Wiki
Jump to: navigation, search

Threads on the .NET Micro Framework is a subset of the implementation on the full .NET framework. It allows you to run multiple pieces of code (semi-)simultaneously. Running multiple threads is called multi threading. A a good example of a multi threading environment are OS's like Windows and Linux which allow you to run multiple programs at the same time. The basics of multi threading in C# are easy but multi threading can cause some extra programming pitfalls.

After reading this article on Threads you should read about Synchronisation.

Contents


Thread Class

The Thread class on .NET Micro Framework looks like this:

public sealed class Thread
{
  // Constructor
  public Thread(ThreadStart start);
 
  // Methods
  public void Start();
  public void Abort();  
  public void Join();
  public bool Join(int millisecondsTimeout);
  public bool Join(TimeSpan timeout);   
 
  // Static methods
  public static AppDomain GetDomain();
  public static void Sleep(int millisecondsTimeout);    
 
  // Properties    
  public bool IsAlive { get; }
  public ThreadPriority Priority { get; set; }
  public ThreadState ThreadState { get; }
 
  // Static properties
  public static Thread CurrentThread { get; }
}

You probably recognize the Sleep method. Even if you don't have a multi threaded application, you may have used it. You can use it to pause execution of the current thread for a specified number of milliseconds.

Start a new Thread

Below, you can see an example program. On line 16 you can see the function that will run simultaneously to the main program. A new Thread is created and started. The main program now continues outputting "Main" to the debug output. The thread we have just started outputs ExampleThread at the same time.

  1. public static void Main()
  2. {
  3.   // Create new Thread that runs the ExampleThreadFunction
  4.   Thread ExampleThread = new Thread(new ThreadStart(ExampleThreadFunction));
  5.  
  6.   // Start our new Thread
  7.   ExampleThread.Start();
  8.  
  9.   // Main program continues, lets do something
  10.   for (int x = 0; x < int.MaxValue; x++) {
  11.     Thread.Sleep(250);
  12.     Debug.Print("Main");
  13.   }
  14. }
  15.  
  16. public static void ExampleThreadFunction()
  17. {
  18.   // Do some fake work
  19.   for (int x = 0; x < int.MaxValue; x++) {
  20.     Thread.Sleep(250);
  21.     Debug.Print("ExampleThread");
  22.   }
  23. }

When you run the above program you should see something like this in the Debug output:

ExampleThread
ExampleThread
Main
ExampleThread
Main
ExampleThread
ExampleThread
Main
Main
ExampleThread
Main
ExampleThread
Main

Waiting for a Thread to complete and stopping a Thread

If you want to hold the current thread until another thread has finished you can call the Join method. It has an optional time-out parameter. With this parameter you can set the maximum time the current thread may block. Besides waiting for a thread to finish, you can also Abort. Calling this method raises an ThreadAbortException in the given thread. Usually, this will terminate the thread.

Below is an example. The program creates a new thread that prints "ExampleThread". Then the main thread waits for 10 seconds. Then the main thread waits for the ExampleThread to finish with a maximum of 30 seconds. If the thread is still running it aborts it.

  1. public static void Main()
  2. {
  3.   // Create new Thread that runs the ExampleThreadFunction
  4.   Thread ExampleThread = new Thread(new ThreadStart(ExampleThreadFunction));
  5.  
  6.   // Start our new Thread
  7.   ExampleThread.Start();
  8.  
  9.   // Wait about 10 seconds
  10.   Thread.Sleep(10000);
  11.  
  12.   // Try to Join the threads with a timeout of 30 seconds
  13.   bool ThreadJoined = ExampleThread.Join(new TimeSpan(0, 0, 30));
  14.  
  15.   // If not succesfully joined Abort thread
  16.   if (ThreadJoined == false)
  17.   {
  18.     // Force abortion
  19.     ExampleThread.Abort();
  20.   }
  21. }
  22.  
  23. public static void ExampleThreadFunction()
  24. {
  25.   // Do some fake work
  26.   for (int x = 0; x < int.MaxValue; x++)
  27.   {
  28.     Thread.Sleep(10);
  29.     Debug.Print("ExampleThread");
  30.   }
  31. }

Passing parameters to a new Thread

One of the things missing in the .NET Micro Framework Thread implementation is passing parameters when starting a Thread. This may seem like a major drawback, but it is easy to overcome. Create a new class that holds the required parameters. Create an instance of it and run the Thread function. Below is an example of this:

  1. public class ThreadWithParameters
  2. {
  3.   // Storage for Parameter
  4.   private String _parameter;
  5.  
  6.   // Constructor
  7.   public ThreadWithParameters(String parameter)
  8.   {
  9.     // Store parameter
  10.     _parameter = parameter;
  11.   }
  12.  
  13.   // Function for ThreadStart
  14.   public void ThreadMethod()
  15.   {
  16.     Debug.Print("Passed parameter: " + _parameter);
  17.   }
  18. }
  19.  
  20. public static void Main()
  21. {
  22.   // Create a new ThreadWithParameters instance
  23.   ThreadWithParameters ParamThread = new ThreadWithParameters("Hello World");
  24.  
  25.   // Create new Thread that runs the ThreadMethod in the ThreadWithParameters instance
  26.   Thread ExampleThread = new Thread(new ThreadStart(ParamThread.ThreadMethod));
  27.  
  28.   // Start our new Thread
  29.   ExampleThread.Start();
  30.  
  31.   // Sleep infinite
  32.   Thread.Sleep(Timeout.Infinite);
  33. }
Personal tools