2

UWP, I can not be disable to suspend. I need your knowledge. Here is my simple example to know issue. It's counter increase/decrease application.

Actually, I want to monitor temperatures from device for 356 x 24 hours without any stopping. I expect to increase value++ during Suspend mode.

but UWP does not work during supending.. Why ??

enter image description here

Uploaded code is here at Github.com. You can test My simple application. https://github.com/bidasknakayama/Basic

here is my environment. It's desktop PC.

  • Desktop PC or Plugged Note PC ( no battery mode )
  • Outputed as Sideloaded application

Package.appxmanifest

<?xml version="1.0" encoding="utf-8"?>                                                                                          
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"                                                
         xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"                                                       
         xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"                                                   
         xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"                  
         xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"                                           
        IgnorableNamespaces="uap mp rescap">                                                                                    


  <Capabilities>                                                                                                                
    <Capability Name="internetClient" />                                                                                        
    <rescap:Capability Name="extendedBackgroundTaskTime"/>                                                                      
    <rescap:Capability Name="extendedExecutionUnconstrained"/>                                                                  
    <DeviceCapability Name="serialcommunication">                                                                               
      <Device Id="any">                                                                                                         
        <Function Type="name:serialPort" />                                                                                     
      </Device>                                                                                                                 
    </DeviceCapability>                                                                                                         
  </Capabilities>                                                                                                                                                                                                                                             
</Package>       

App.xaml.cs https://github.com/bidasknakayama/Basic/blob/master/Basic/App.xaml.cs

I tried to insert ExtendedExecutionSession or ExtendedExecutionForegroundSession at position (A)(B)(C)(D). but Not success.

BaseViewModel.cs https://github.com/bidasknakayama/Basic/blob/master/Basic/ViewModels/BaseViewModel.cs

    protected override void OnActivate()
    {
        base.OnActivate();
        _eventAggregator.Subscribe(this);

        // Background Run.
        Task.Run(MainLoop);

    }
    private async Task MainLoop()
    {
        while (true)
        {
            Counter = _counter + 1;
            await Task.Delay(500);
            await ImportantNoStopJob();
        }
    }
    private async Task ImportantNoStopJob() // Important. I do not want to stop it during suspend.
    {
        Temp = _temp  - 1;
        await Task.Delay(500);
    }

Question is simple.

How to increase value During Suspending ??

Update #1

With Raymond's advice (Thank you!), I found good article.. It has event of CoreWindow.Activated.

Window.Current.CoreWindow.Activated += CoreWindow_OnActivated;

With this code, I got ExtendedExecutionResult.Allowed. It's good.

but I still have problem. When release mode, I tried to simulate "Suspending". The code never work.. I think. my background job tried to modify UI. then, code may not work.. but I do not know why...

enter image description here

and I upload ver 2 code , You can test at easy.

https://github.com/bidasknakayama/Basic2

Update #2

I thought, We should not use "Suspended" button for this test. It make true-suspended Even if we write this code. Right ? if so, We may resolve this problem now ...

I tested some, It look like good.

Kazuhiko Nakayama
  • 801
  • 1
  • 8
  • 24
  • None of the places is correct. Methods whose names begin with `Request` must be called from a UI thread. In (A) and (B), you haven't created your window yet, so the thread is not a UI thread. In (C) and (D), you are calling it from the suspending handler, which is not guaranteed to be raised on any particular thread, so you might not be on the UI thread. Create the extended session after you activate your window. – Raymond Chen Dec 01 '17 at 04:51
  • Thank you , Raymond. I run when Window.Active.. but During Suspending, The code never work. I think, My code try to update UI. Is this cause ? Please check my "Update 1" in Question. I wrote detail. – Kazuhiko Nakayama Dec 01 '17 at 06:05
  • You cannot run indefinitely in a suspend. The extended execution for suspension is to give you extra time to save your data before suspending, but you cannot postpone suspension indefinitely. (You wouldn't want to, because your program cannot resume until the suspension completes. If you never complete the suspension, then your program will never resume.) – Raymond Chen Dec 01 '17 at 15:09
  • With Desktop ( Plugged ) PC, To make a monitoring application with UWP, Is it not possible ? I could not understand...Please advice me.. Should we ask user NOT to do minimized application ? or Can user run application with Minimized for 365 x 24 hours ? Should we use WPF ? but it's not possible.I spend 6 month for the UWP development.... I need your advice... – Kazuhiko Nakayama Dec 02 '17 at 09:18
  • Acquire the extended execution when you are active. The extended execution will prevent you from suspending. – Raymond Chen Dec 02 '17 at 14:27

0 Answers0