Issue with Power.Shutdown

Using the following code on a SC20260E SOM :

private static void TestShutdown()
        {
            new Thread(BlinkLeds).Start();
            Thread.Sleep(4567);
            Power.Shutdown(false, DateTime.Now.AddSeconds(15));
        }

I get this exception :

#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (1) ####
#### Message: 
#### GHIElectronics.TinyCLR.Native.Power::SetLevel [IP: 0000] ####
#### GHIElectronics.TinyCLR.Native.Power::Shutdown [IP: 0028] ####
#### RamBoardTests.Program::TestShutdown [IP: 002f] ####
#### RamBoardTests.Program::Main [IP: 0004] ####
Exception levée : 'System.InvalidOperationException' dans GHIElectronics.TinyCLR.Native.dll
Une exception non gérée du type 'System.InvalidOperationException' s'est produite dans GHIElectronics.TinyCLR.Native.dll

Am I doing something wrong ? (code taken from the docs)

Can you check if Rtc is working on that board, just run code below to see if any exception.

var rtc = RtcController.GetDefault();

Rtc is working and is valid.

you have to set Rtc first. If not set the year is wrong then Now.Add(15) is invalid.

Just have tested, see your issue. Set RTC first and it is good to go.

static void Test_Custom()
        {
            var rtc = RtcController.GetDefault();
                       
            rtc.SetTime(RtcDateTime.FromDateTime(new DateTime(2018, 2, 2, 10, 9, 8)));

            new Thread(BlinkLeds).Start();
            Thread.Sleep(4567);
            Power.Shutdown(true, DateTime.Now.AddSeconds(15));

            Thread.Sleep(-1);
        }

        static void BlinkLeds()
        {
            while(true)
            {
                Thread.Sleep(100);
            }

        }
1 Like

Thank you very much ! I now understand that I was doing wrong with the “time” it should shut down.

Since my Rtc is valid, I have used this code to test (successfully) :

private static void TestShutdown()
        {
            new Thread(BlinkLeds).Start();
            Thread.Sleep(4567);
            Power.Shutdown(false, RtcController.GetDefault().Now.AddSeconds(15));
        }

Edit : What is the exact relation between Rtc and this shutdown ?
DateTime.Now was indeed different than System.Time but what happen if there is no Rtc ?

Maybe docs should explain that shutdown users RTC and so it must be set @Greg_Norris

No, not really, TinyCLR should handle this case.

There is a ton of cases can be happened in real life, we are trying to handle as much as we can.

Like IFU, we discussed internally few weeks to assume a lot of complex cases can be happened, but finally forgot a simple case, a bitmap can be a lot of white 0xFF.

You guys here is pointing them out for us.

2 Likes

Then do not forget Power.Sleep :wink: