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 |
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.
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.
public static void Main()
{ // Create new Thread that runs the ExampleThreadFunctionThread ExampleThread = new Thread(new ThreadStart(ExampleThreadFunction));
// Start our new ThreadExampleThread.Start();
// Main program continues, lets do somethingfor (int x = 0; x < int.MaxValue; x++) {
Thread.Sleep(250);
Debug.Print("Main");
}}public static void ExampleThreadFunction()
{ // Do some fake workfor (int x = 0; x < int.MaxValue; x++) {
Thread.Sleep(250);
Debug.Print("ExampleThread");
}}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
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.
public static void Main()
{ // Create new Thread that runs the ExampleThreadFunctionThread ExampleThread = new Thread(new ThreadStart(ExampleThreadFunction));
// Start our new ThreadExampleThread.Start();
// Wait about 10 secondsThread.Sleep(10000);
// Try to Join the threads with a timeout of 30 secondsbool ThreadJoined = ExampleThread.Join(new TimeSpan(0, 0, 30));
// If not succesfully joined Abort threadif (ThreadJoined == false)
{ // Force abortionExampleThread.Abort();
}}public static void ExampleThreadFunction()
{ // Do some fake workfor (int x = 0; x < int.MaxValue; x++)
{Thread.Sleep(10);
Debug.Print("ExampleThread");
}}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:
public class ThreadWithParameters
{ // Storage for Parameterprivate String _parameter;
// Constructorpublic ThreadWithParameters(String parameter)
{ // Store parameter_parameter = parameter;
} // Function for ThreadStartpublic void ThreadMethod()
{Debug.Print("Passed parameter: " + _parameter);
}}public static void Main()
{ // Create a new ThreadWithParameters instanceThreadWithParameters ParamThread = new ThreadWithParameters("Hello World");
// Create new Thread that runs the ThreadMethod in the ThreadWithParameters instanceThread ExampleThread = new Thread(new ThreadStart(ParamThread.ThreadMethod));
// Start our new ThreadExampleThread.Start();
// Sleep infiniteThread.Sleep(Timeout.Infinite);
}