Parsa Karami`s Blog

an Enthusiastic Windows Delevoper

Power Management in Windows 10 Iot

772 days ago on July 14, 2017   |   Parsa Karami on Universal Windows App   |   1845
     Power Management in Windows 10 Iot     

For some cases you need control power management of system, sometimes you need relaunch the app for some reasons or you need shutdown system remotly, maby you register a scheduled tasks on windows startup for some system setting changes like set new ip address or system datetime,The reasons that why you need access to power management of system is not so important in this point, the main goal of this post is how can use it practically. for beginning let use the Windows.System namespace, first add it in the page.

Nowadays Iot devices makes our living so easier, devices are so widespread and problems will easily solved than before, when microsoft realesed windows 10 on July 29, 2015 and revealed the Universal Windows Platform (UWP) approach the era of mbedded  os was begin, remember the Windows Embedded family and it concerns, now easily you can developed universal windows apps that run on iot devices, you can see supported devices that can run universal windows apps.

I don`t want to point more about universal windows apps and iot approach here, so let get dive into title.
Consider a problem of project that must be solve and need iot solution for more details let me describe, for solving the hypothetical problem you need configure and setup multiple mini-computer board in different areas, this areas are far from each other, for example consider they spread all over a state or all over a country or maby all over the world, in real world you can assumed a weather forecasting services like AccuWeather app and its weather stations use mini-computer boards like RaspberryPI or Qualcomm Dragonboard and other same board that can use Windows 10 Iot Core, you can see list of available devices here. these boards place in each weather station.

After setup boards and configure them, now for some cases you need control power management of system, sometimes you need relaunch the app for some reasons or you need shutdown system remotly, maby you register a scheduled tasks on windows startup for some system setting changes like set new ip address or system datetime (until writing this post microsoft added a class for windows iot extention in .net framework that can change system datetime this class name is DateTimeSetting but it`s now has prereleased labled, for changing ip address of os there is no direct solution you can use a scheduled tasks on startup and change it using Netsh Command) .

The reasons that why you need access to power management of system is not so important in this point, the main goal of this post is how can use it practically. for beginning let use the Windows.System namespace, first add it in the page.

     using Windows.System;

For using ShutdownManager class and it functionality you must first refer Windows IoT Extensions for the UWP to ypur project 

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


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

The system restart or shutdown after the timeout that you pass as second argument to static BeginShutdown method the first argument defines type of action that occur in BeginShutdown method it`s a enumaration type that has two value 

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

Now the time that run and debug the app to shutdown or restart system 

Click restart button and go to visual studio for see what happen, in below picture you see an exception when BeginShutdown method was called

The exception has occur and the reason is my app doesn't have permission to control system managment, so what should i do?
open your Package.appxmanifest file to adding permisson for this app first add below line in Package tag

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

Then add this line in Capabilities tag 

     <iot:Capability Name="systemManagement"/>

Then run app again and see the result, the system successfully restart, you can also use CancelShutdown to cancel a shutdown of fixed-purpose device that is already in progress.

     ShutdownManager.CancelShutdown();