Slow down application

Share your advanced PureBasic knowledge/code with the community.
codeprof
User
User
Posts: 65
Joined: Sun Sep 16, 2012 12:13 pm

Slow down application

Post by codeprof »

Code snipped to slow down the execution of the current thread.
Don't know if this is useful (maybe for testing, or if your application runs to fast :lol: )

Code: Select all

;Licence: Public domain
EnableExplicit
Structure THROTTLE_CPU
  hThread.i
  pauseFactor.i
EndStructure  

Procedure __ThPause(*params.THROTTLE_CPU)
  Protected timecaps.TIMECAPS, exitCode.i   
  If *params <> #Null
    timeGetDevCaps_(timecaps,SizeOf(TIMECAPS))  
    timeBeginPeriod_(timecaps\wPeriodMin)
    Repeat
      If GetExitCodeThread_(*params\hThread, @exitCode)      
        If exitCode <> #STILL_ACTIVE
          ; for the main thread this would be to late(process already terminated) however we could also use only repeat...forever here       
          Break
        EndIf 
      EndIf  
      SuspendThread_(*params\hThread)
      Sleep_(*params\pauseFactor)  
      ResumeThread_(*params\hThread)
      Sleep_(1)
    ForEver
    timeEndPeriod_(timecaps\wPeriodMin)
    CloseHandle_(*params\hThread)
    FreeMemory(*params)
  EndIf
EndProcedure

Procedure ThrottleCPU(pauseFactor.i)
  Protected *params.THROTTLE_CPU = AllocateMemory(SizeOf(THROTTLE_CPU))
  If *params
    *params\pauseFactor = pauseFactor
    If DuplicateHandle_(GetCurrentProcess_(),GetCurrentThread_(), GetCurrentProcess_(), @*params\hThread, #THREAD_GET_CONTEXT, #False, #DUPLICATE_SAME_ACCESS)
      ProcedureReturn CreateThread(@__ThPause(), *params) 
    Else
      FreeMemory(*params)
    EndIf  
  EndIf
  ProcedureReturn #Null
EndProcedure



;Example
ThrottleCPU(100)

OpenWindow(0, 0, 0, 800, 800, "Highspeed drawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  
  Repeat
    StartDrawing(WindowOutput(0))
      LineXY(Random(800),Random(800),Random(800),Random(800),Random($FFFFFF))
    StopDrawing()
  Until WindowEvent() = #PB_Event_CloseWindow
Last edited by codeprof on Thu Apr 11, 2013 8:43 pm, edited 1 time in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Slow down application

Post by Kwai chang caine »

Works great....thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Slow down application

Post by Nubcake »

Is there such a thing as 'too fast' ? :D
Post Reply