UWP suspend disable with ExtendedExecutionSession

2020-07-28 10:44发布

问题:

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 ??

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...

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.

标签: uwp suspend