Multithreading with Netduino and .NET Microframework – part 3


(to part 1, part 2 or part 4)

In addition to the previously mentioned methods for synchronisation of threads, Microframework provided two classes to do this, using events: AutoResetEvent and ManualResetEvent. Both classes inherit WaitHandle and only differ by the fact that you have to manually reset status when using ManualResetEvent. Let’s see some examples of using those classes:

public class Program
{
     // Initialise auto reset event with not signalled (false) initial state
     private static AutoResetEvent autoResetEvent = new AutoResetEvent(false);

     static void Main()
     {
          Thread someThread = new Thread(SomeMethod);
          someThread.Start();
          ...
          // Waiting for thread's notification
          autoResetEvent.WaitOne();
          ...
          // This is auto reset event so no reset is required
          someThread.Start();
          autoResetEvent.WaitOne();
          ...
     }

     private static void SomeMethod()
     {
          ...
          // Send notification
          autoResetEvent.Set();
     }
}

or

public class Program
{
     // Initialise manual reset event with not signalled (false) initial state
     private static ManualResetEvent namualResetEvent = new ManualResetEvent(false);

     static void Main()
     {
          Thread someThread = new Thread(SomeMethod);
          someThread.Start();
          ...
          // Waiting for thread's notification
          namualResetEvent.WaitOne();
          ...
          // This is manual reset event so reset is required
          namualResetEvent.Reset();
          someThread.Start();
          namualResetEvent.WaitOne();
          ...
     }

     private static void SomeMethod()
     {
          ...
          // Send notification
          namualResetEvent.Set();
     }
}

WaitHandle class (thus both AutoResetEvent and ManualResetEvent classes too) have two more interesting and flexible methods – WaitAll() and WaitAny(). First waits for all the elements in the specified array to receive a signal and second waits for any of the elements to receive a signal. Note that they accept array of WaithHandles thus way you can create and work with many threads and synchronise between them. Both methods has overrides that allow specifying the number of milliseconds to wait and parameter to specify shall thread(s) to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it afterwards.

You can set the thread priority using ThreadPriority enumeration and check thread status using ThreadState enumeration. TheadState allows you to perform bitwise operations so you can check for multiple statuses:

if (someThread.ThreadState == (ThreadState.Suspended | ThreadState.Stopped))
{
     ...
}

Lastly, we must not leave Timer class without attention 🙂 This is very useful class that “Provides a mechanism for executing a method at specified intervals”. It is simple as it states. Timer’s constructor uses TimerCallback delegate for the method you want to execute and there are two handy overrides, which allow us to specify times intervals using int (in milliseconds) or TimeSpan. The second parameter is also useful in case you want to pass additional information to the executed method. Keep it null if no information is to be passed.

public class Program
{
     static void Main()
     {
          // Initial delay 5s, invoke every 1 second after that
          var someTimer = new Timer(new TimerCallback(SomeMethod), "Some data", 5000, 1000);
          // Or
          //var someTimer = new Timer(new TimerCallback(SomeMethod), "Some data", new TimeSpan(0, 0, 0, 5), new TimeSpan(0, 0, 0, 1));
          ...
          Console.ReadLine();
     }

     private static void SomeMethod(object data)
     {
          ...
          Console.WriteLine((string)data);
          ...
     }
}

One good thing about last version Microframework is that we can use lambda expressions:

public class Program
{
     static void Main()
     {
          var someTimer = new Timer(new TimerCallback(
               (object data) =>
               {
                    Console.WriteLine((string)data);
               }),
               "Some data", new TimeSpan(0, 0, 0, 5), new TimeSpan(0, 0, 0, 1));
          ...
          Console.ReadLine();
     }
}

Last note to take is that Microframework’s System.Threading namespace does not implement all the classes of the full .NET framework System.Threading.

(to part 1, part 2 or part 4)

Advertisements

4 comments on “Multithreading with Netduino and .NET Microframework – part 3

  1. Pingback: Georgi's blog

  2. Pingback: Multithreading with Netduino and .NET Microframework – part 2 « Georgi's blog

  3. Pingback: Multithreading with Netduino and .NET Microframework – part 1 « Georgi's blog

  4. Pingback: Multithreading with Netduino and .NET Microframework – part 4 « Georgi's blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s