We said we weren’t going to turn this into a C# tutorial, but a few words about the program architecture are appropriate.

Windows IoT Core applications are oriented around tasks. If you’re mostly familiar with Arduino or python programming, you probably aren’t too familiar with tasks. Basically, a task is a program than can run at the same time as other programs on the same computer. Arduino only allows one program at a time. Linux supports tasks, but you may not have done any task-oriented programming.

We’re not going to go into the intricacies of tasks here, but it’s worth noting that the fundamental structure of our BlinkyHeadless application is a task. You can see this from the type of the StartupTask class – it inherits from the IBackgroundTask interface class. To execute BlinkyHeadless, Windows IoT creates a new task object and uses it to call the Run function.

The Run function is where we’ll put our code.

If you’re curious, you can read more about background tasks here:

For the purposes of this tutorial, we're going to use screen captures to illustrate the code components of the application.  The full code will be available at the end of the tutorial.

Timer thread

We’re not quite done with tasks. Tasks may also be referred to as threads, particularly when they exist within a task. One of the standard thread objects in Windows is the Timer. We will be using a timer to control the blinking of our LED.

If you are familiar with Arduino, you will be accustomed to using delay() statements to control things like blinking LEDs. The drawback of this approach is that the Arduino can’t do anything else while it is waiting for the delay to expire.

In Windows, instead of using a delay, we can start a timer thread. Once the thread is started, we can go about doing other useful things, and the timer will alert us when the period has expired. The timer does this through a Timer Tick event. All we need to do is to create an event function and associate it with the timer. When it’s time to turn the LED on or off, windows will automatically call our tick event function.

There’s one other thing we need to do. You may have noticed a series of using statements at the top of StartupTask.cs. These are similar to the C #include or python import statements. They identify libraries we want to refer to in our program. We need to add the threading library to the list:

Don’t worry about the using statements that are grayed out – it just means that there are no references to those libraries in the code yet.

Task deferral

There is one other thing about tasking in BlinkyHeadless, and this is subtle. When the Run function exits, so does our main task. That is, our BlinkyHeadless application terminates. We don’t want this to happen.

In BlinkyHeadless, we really don’t have anything else to do while we’re waiting for the timer tick event. We could add an infinite loop, but that will consume processor resources. Windows provides a better way to do the same thing, and that is a Task Deferral.

When we set up a deferral on a background task, such as our main task, the task won’t exit until we explicitly tell it that we’re done, even if it runs out of other things to do.

You can read more about background task deferral here:

This guide was first published on Aug 03, 2016. It was last updated on Aug 03, 2016.

This page (Program Architecture) was last updated on Jul 14, 2016.

Text editor powered by tinymce.