App Nap

Mac OSX specific forum
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

App Nap

Post by Rinzwind »

It seems that macOS still naps an app even if it requested not too?

Code: Select all

EnableExplicit

Global w, e, a


; Author : Danilo
; Date   : 25.03.2014
; Link   : https://www.purebasic.fr/english/viewtopic.php?f=19&t=58828
; Info   : NSActivityOptions is a 64bit typedef - use it with quads (.q) !!!

#NSActivityIdleDisplaySleepDisabled             = 1 << 40
#NSActivityIdleSystemSleepDisabled              = 1 << 20
#NSActivitySuddenTerminationDisabled            = (1 << 14)
#NSActivityAutomaticTerminationDisabled         = (1 << 15)
#NSActivityUserInitiated                        = ($00FFFFFF | #NSActivityIdleSystemSleepDisabled)
#NSActivityUserInitiatedAllowingIdleSystemSleep = (#NSActivityUserInitiated & ~#NSActivityIdleSystemSleepDisabled)
#NSActivityBackground                           = $000000FF
#NSActivityLatencyCritical                      = $FF00000000

Procedure BeginWork(Option.q, Reason.s= "MyReason")
  Protected NSProcessInfo = CocoaMessage(0,0,"NSProcessInfo processInfo")
  If NSProcessInfo
    ProcedureReturn CocoaMessage(0, NSProcessInfo, "beginActivityWithOptions:@", @Option, "reason:$", @Reason)
  EndIf
EndProcedure

Procedure EndWork(activity)
  Protected NSProcessInfo = CocoaMessage(0, 0, "NSProcessInfo processInfo")
  If NSProcessInfo
    CocoaMessage(0, NSProcessInfo, "endActivity:", activity)
  EndIf
EndProcedure

w = OpenWindow(0, 0, 0, 400, 300, "Test")
a = BeginWork(#NSActivityUserInitiatedAllowingIdleSystemSleep, "No napping")
Debug a

Repeat
  e = WaitWindowEvent()
Until e = #PB_Event_CloseWindow

EndWork(a) ;Invalid memory access...
ps. EndWork causes a crash in this small example, but not when put directly after BeginWork.

Any way to make it work as intended? As in no app napping as seen in activity monitor/Energy when app is in background and other app active/used?
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: App Nap

Post by mk-soft »

Its only for threads, not work at MainScope

Code: Select all

EnableExplicit

Global w, e, a


; Author : Danilo
; Date   : 25.03.2014
; Link   : https://www.purebasic.fr/english/viewtopic.php?f=19&t=58828
; Info   : NSActivityOptions is a 64bit typedef - use it with quads (.q) !!!

#NSActivityIdleDisplaySleepDisabled             = 1 << 40
#NSActivityIdleSystemSleepDisabled              = 1 << 20
#NSActivitySuddenTerminationDisabled            = (1 << 14)
#NSActivityAutomaticTerminationDisabled         = (1 << 15)
#NSActivityUserInitiated                        = ($00FFFFFF | #NSActivityIdleSystemSleepDisabled)
#NSActivityUserInitiatedAllowingIdleSystemSleep = (#NSActivityUserInitiated & ~#NSActivityIdleSystemSleepDisabled)
#NSActivityBackground                           = $000000FF
#NSActivityLatencyCritical                      = $FF00000000

Procedure BeginWork(Option.q, Reason.s= "MyReason")
  Protected NSProcessInfo = CocoaMessage(0,0,"NSProcessInfo processInfo")
  If NSProcessInfo
    ProcedureReturn CocoaMessage(0, NSProcessInfo, "beginActivityWithOptions:@", @Option, "reason:$", @Reason)
  EndIf
EndProcedure

Procedure EndWork(activity)
  Protected NSProcessInfo = CocoaMessage(0, 0, "NSProcessInfo processInfo")
  If NSProcessInfo
    CocoaMessage(0, NSProcessInfo, "endActivity:", activity)
  EndIf
EndProcedure


Procedure thWork(*Exit.Integer)
  Protected no_nap
  no_nap = BeginWork(#NSActivityUserInitiatedAllowingIdleSystemSleep)
  
  Repeat
    If *Exit\i
      Break
    EndIf
    Delay(100)
  ForEver
  
  EndWork(no_nap)
EndProcedure

w = OpenWindow(0, 0, 0, 400, 300, "Test")

Define exit
Define thread = CreateThread(@thWork(), @exit)

Repeat
  e = WaitWindowEvent()
Until e = #PB_Event_CloseWindow

exit = #True
WaitThread(thread)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: App Nap

Post by Rinzwind »

Good to know. And works as expected in Catalina.
Post Reply