CreateTask

Share your advanced knowledge/code with the community.
User avatar
eddy
Posts: 124
Joined: Thu Mar 27, 2014 8:34 am

CreateTask

Post by eddy »

- function to delay task
- function to repeat task (option to stop repeating, loop counter)

Code: Select all

Procedure CreateTask(*OnExecutionFunction, Interval, Delay=0, *Parameter=0)
  !var task={ 
  !  parameter:p_Parameter, callFunc:p_OnExecutionFunction, 
  !  startTime:0, endTime:0, duration:0, interval:0, count:0, intervalId:0, isRunning:false, 
  !  execution:function(){    
  !    if (!this.isRunning){ //endif
  !      this.isRunning = true;  
  !      this.startTime = new Date().getTime();
  !      this.interval = this.startTime-this.endTime;
  !      this.count++;        
  !      this.callFunc(this.parameter, this.count, this.startTime, this.interval, this.endTime, this.duration) && clearInterval(this.intervalId);
  !      this.endTime = new Date().getTime();
  !      this.duration = this.endTime-this.startTime;          
  !      this.isRunning = false;
  !    }
  !  }
  !};         
  !setTimeout(function(){ task.intervalId=setInterval(function(){ task.execution(); },v_Interval); },v_Delay);
EndProcedure


CompilerIf #PB_Compiler_IsMainFile 
  ; ***************************
  ; EXAMPLE 
  ; ***************************
  
  Procedure.i TaskRunning(*Parameter.String, ExecutionCount, ExecutionStartTime, ExecutionInterval, PreviousExecutionEndTime, PreviousExecutionDuration)
    Debug "Param = "+*Parameter\s
    Debug "Execution Count ="+ExecutionCount
    Debug "Execution Start Time = "+ExecutionStartTime
    Debug "Interval = "+ExecutionInterval
    Debug "Previous Execution End Time = "+PreviousExecutionEndTime
    Debug "Previous Execution Duration = "+PreviousExecutionDuration
    Debug ""
    
    stop = Bool(ExecutionCount>=10)
    ProcedureReturn stop ;// return TRUE to stop execution loop
  EndProcedure
  
  
  param1.String
  param1\s="Task 1"
  param2.String
  param2\s="Task 2"
  CreateTask(@TaskRunning(), 500, 1500, @param1)
  CreateTask(@TaskRunning(), 900, 1000, @param2)  
CompilerEndIf 
Last edited by eddy on Thu May 08, 2014 8:20 pm, edited 2 times in total.
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: DelayProcedure, RepeatProcedure

Post by Fred »

Is it similar to AddWindowTimer() or I miss something ?
User avatar
eddy
Posts: 124
Joined: Thu Mar 27, 2014 8:34 am

Re: CreateThreadedTask

Post by eddy »

I merged these two functions.
It's more like CreateThread + delay + interval.
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: CreateThreadedTask

Post by Fred »

Is it really a thread ?
User avatar
eddy
Posts: 124
Joined: Thu Mar 27, 2014 8:34 am

Re: CreateThreadedTask

Post by eddy »

Fred wrote:Is it really a thread ?
You're right. It's not a thread.
The proper way to create thread is to use Web Worker
Post Reply