Profile image - Parsa Karami website

Parsa Karami`s Blog

Life runs on code.

Power Management in Windows 10 Iot

Profile image - Parsa Karami website
2471 days ago on July 14, 2017   |   Parsa Karami on Universal Windows App   |   4624
Power Management in Windows 10 Iot - Parsa Karami website
     Power Management in Windows 10 Iot     

In IoT applications, you need to control the power of your system or your board. Regardless of the reasons, you need to restart your board, or you need to do shut down the system programmatically. In this post, I will show you how to do that.

Nowadays, IoT devices make our life easier. IoT devices are so widespread, and they can solve our problems around. When Microsoft released Windows 10 in 2015 and revealed the Universal Windows Platform (UWP) approach, the new era of embedded os just started. Remember the Windows Embedded family and its concerns. With the newly introduced strategy, you can develop the Universal Windows apps and run them on IoT devices. You can see supported devices that support Windows 10 IoT and run universal Windows apps.

I do not want to point out more about universal windows apps and the IoT approach here, so let's start.

Consider a project that you can handle it using IoT solutions. You need to configure and set up multiple mini-computer boards. The areas where you want to deploy the mini-computers are far from each other. For example, consider they spread all over a state or a country. As a real-world example, you can assume a weather forecasting service like the AccuWeather app, and its weather stations use mini-computer boards like RaspberryPI or Qualcomm Dragonboard. These boards place in each weather station.

After setting up the boards and configuring them, you need to control the power management of devices. The reasons why you need access to power management of the system are not so important at this point. The leading goal of this post is how can use it practically. To start, we add the Windows.System namespace with the following using statement at the top of the page.

     using Windows.System;

For using ShutdownManager class, you have to make a reference to Windows IoT Extensions for the UWP assembly in your project.

Now you can use the below code when and where you want to Restart or Shutdown the system.

     ShutdownManager.BeginShutdown(ShutdownKind.Restart, TimeSpan.FromSeconds(5));
     ShutdownManager.BeginShutdown(ShutdownKind.Shutdown, TimeSpan.FromSeconds(5));

The system will restart or shut down after the timeout that you pass as the second argument to the static BeginShutdown method. The first argument points to the type of action that occurs in the BeginShutdown(). It is an enumeration type that has two values. 

    public enum ShutdownKind
    {
        Shutdown = 0,
        Restart = 1
    }

Now we can run the application to test the shutdown and restart functions in the device.

Click on the Restart button and go to visual studio to see what happens. You will see an exception when the BeginShutdown method will call.

The exception has occurred. The reason is the application doesn't have permission to control system management. So what should I do? to solve the problem, open your Package.appxmanifest file. Then add permission for this app. Add the below line under the Package tag.

    xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"

Then add this line in the Capabilities tag 

    <iot:Capability Name="systemManagement"/>

Then re-run the application to see the result. The system will restart successfully. Moreover, You can use CancelShutdown() to cancel a shutdown or a restart operation that is already in progress.

     ShutdownManager.CancelShutdown();