Windows IoT Background App

April 3, 2016

Introduction

So now it's time to start developing for the Raspberry Pi 3 using the Windows IoT Core.  There is a fundamental understanding you must be explained here.  These projects can only be used with the UWP (Universal Windows Platform).  This means that any projects that you create must only be compatible with the UWP.  No Full .NET Libraries are compatible.  If you are writing a whole library for a project that spans over multiple types of projects be sure to use Universal Class Libraries so that code can be shared properly.  The .NET Framework Libraries can utilize Universal Libraries, but not the other way around.

Assuming that you have a Raspberry Pi setup and you are able to access it via the name or IP, we can start building our App.  (If not, read the first article in the series Windows IoT on the Pi3.

We will be creating a background app to start.  A background app acts like a Windows Service or Scheduled Task.  You can have it start-up when the Pi powers on and will run until completion.  Once it is completed though, it will end (much like a scheduled task) and the only way to start it again is externally (via the web dashboard or restarting the Pi).  This post will provide the steps to creating a background app for Windows IoT.

Creating your first IoT Background App

The following are the steps that I followed to creating, deploying and debugging on my Raspberry Pi 3 Model B w/ Windows 10 IoT.

Reference(s):

http://ms-iot.github.io/content/en-US/win10/BackgroundApplications.htm

http://ms-iot.github.io/content/en-US/win10/AppDeployment.htm

 

Note: I am using Visual Studio 2015 Community Edition

1. Download Project Templates

For background apps, it is easier to simply download the project templates to make this easier.  The Core Project Templates can be found below (in the Visual Studio Gallery):

Windows IoT Core Project Templates

Once it is downloaded, install by double-clicking the VSIX and the extension will be installed.  Restart VS 2015.

2. Create a New Project

Next, let's create the project.  With the new templates installed, you should be able to find the IoT Core Background Application under Visual C# > Windows > Windows IoT Core.

Name it whatever and click 'OK'.

You will be prompted with a selection of the Build you wish to be compatible with.  I used the latest build (Build 10586).  I also want the minimum version to be the same so we don't lose functionality.

Click 'OK' and the project will be created.

 

3. Let's Write Some Code

For a background application, it can be a bit difficult to do a Hello World.  I'm attempting to just get this app to where I can debug it, so I edited the code to the most basic place:

Let's talk about this.

The entry point of the application is the Run routine.  You'll notice an async keyword added on.  I would always suggest working with the async-await pattern, so I added this on.

In order to use this though, we must create a Deferral to wait until everything is complete.  That is where the taskInstance.GetDeferral() comes from.

In order to wait, call deferral.Complete() after your code calls.

4. Deploy to the Raspberry Pi 3

With Visual Studio, deploying is a fairly easy task.  But we need to change a few things first.

The first thing we need to do is change to a Remove Debugger.  This can be done in 1 of 2 ways:

  1. Change from Local Machine to Remote Machine in the top toolbar.
  2. or Go to the Project's Properties > Debug Tab > Start Options

I elected for option 2.

You need to put the IP of the Remote Machine (your Raspberry Pi) with Universal Authentication.  These will be the settings for the initial deploy.  Each subsequent deploy will have different settings.  This is because the first deploy copies down the appropriate remote debugging dependencies for Visual Studio.

Now the Raspberry Pi 3 is has an ARM Processor Architecture.  This must be set in the General Section of the Build Tab.   Set Platform Target to ARM.

Once this is done, go to Build > Deploy Solution and the process will begin.

Since it may be deploying everything including remote debugger dependencies, this could take a couple minutes.  Wait it out!  Once it is complete, we can verify everything got their safely.

 

5. Verify Deploy

Before we continue, let's verify our deployment.  In your browser, go to the ip address of your Pi (with port 8080) and log into your Web Dashboard.

Once there, go to the Apps section of the Dashboard.  You should see your new app in the top table.  In my case it is SparcPoint.IoT.TestApp.  We can verify it is a Background App based on the App Type column.  If desired, we could add this app to occur during startup as well (which we won't right now).

6. Enable Remote Debugging

Under the Debugging section there is a Start button that will "Start Visual Studio Remote Debugger."  Click it!!

When clicked, you'll notice a pop-up probably similar to this:

This will give you the new information you must enter for subsequent deployments of the app.  In my case, it is the IP with Port 8116.  Go Make these changes in the Project Settings.

As you can see, I also changed the Authentication to None.  In my tests, I was receiving the error DEP6953 and changing it to None fixed the issue.

7. Debug Time

Now go set some break points in your code and hit Run (F5).  You should see your code stop when it hits the break-point.

And blam! You are now debugging your Raspberry Pi 3!

I admit, I have yet to figure out (if you can at all) add variable watches or step through code line by line.  I could understand if you were not able to do this due to the nature of remote debugging.  However, I'm new to this type of debugging.  It seems to be so much easier when it's local!

Hope this helps, and good luck on your project!

Back to blog

Related Posts

Check out our thoughts here.

What is important to a career

Lately I’ve been spending a lot of time thinking about my career and where it’s going. I don’t want to give the impression that I have never thought about my career before, but now the thoughts are becoming constant.

May 8, 2018
Databases: Component or Infrastructure?

There is always strong debate around databases and their role in development. Sometimes they are considered components, while others will consider them infrastructure. Is there a right answer? Let's discuss!

March 15, 2018
Software Maintenance: The Never-Ending Feud

There is one, and only one, primary focus that any software developer acknowledge: the ability for software to be maintainable. Of course, correctness, functionality, and performance are all important, these will always be easier to address with maintainable software.

January 25, 2018