A rudimentary timer (low granularity), that doesn't lock IGOR up.

Sometimes when you are doing something that depends on waiting for a period of time (such as data acquisition) you need a timer that doesn't lock IGOR up. This timer can be started by calling startwait, with a given wait period. You can then use waitstatus() (probably from another background task) to see if the timer has finished. If waitStatus returns 1, then the waiting period isn't over. You can stop the waiting period by calling stopWait(). This has low granularity, as the background task fires once a second.

//a background task that you can use to wait for a given timeperiod.  This is probably called from
//another background task.  This is useful if you don't want to lock IGOR up.

Function startwait(timeout)
    variable timeout
    variable/g root:endtime
    NVAR endtime = root:endtime

    endtime = datetime+timeout

    CtrlNamedBackground waiter,period=60,proc=waittask
    CtrlNamedBackground waiter, start
End

Function stopwait()
    ctrlnamedbackground waiter,stop=1
    killvariables root:endtime
ENd

Function waitStatus()
    ctrlnamedbackground waiter,status
    return numberbykey("RUN",S_info)
End

Function waittask(s)
    STRUCT WMBackgroundStruct &s
    NVAR endtime = root:endtime
    if(datetime > endtime)
        stopwait()
        return 1
    endif
    return 0
End
//a background task that you can use to wait for a given timeperiod.  This is probably called from
//another background task.  This is useful if you don't want to lock IGOR up. (original by andyfaff)
//Upgrades by Stefan Mangold, using multiple alarms, executing one task per timer


// startwaitAndExecuteTaskUnified, best option

Function startwaitAndExecuteTaskUnified(timeout,type,TaskName)
    variable timeout, type
    string TaskName
    if(exists("root:numberOfWaiter")==0)
        variable/g root:numberOfWaiter
        NVar numberOfWaiterG = root:numberOfWaiter
        numberOfWaiterG = 0
    else
        NVar numberOfWaiterG = root:numberOfWaiter
    endif  
   
   
    if(numberOfWaiterG == 0)
        make/D/O/N=(numberOfWaiterG+1) root:endtimeW
        make/D/O/N=(numberOfWaiterG+1) root:typeW
        make/T/O/N=(numberOfWaiterG+1) root:TaskNameW
    else
        redimension /N=(numberOfWaiterG+1) root:endtimeW
        redimension /N=(numberOfWaiterG+1) root:typeW
        redimension /N=(numberOfWaiterG+1) root:TaskNameW
    endif
       
    wave endtimeW = root:endtimeW
    wave typeW = root:typeW
    wave/T TaskNameW = root:TaskNameW
   
   
    TaskNameW[numberOfWaiterG] = TaskName
    typeW[numberOfWaiterG] = type
    endtimeW[numberOfWaiterG] = datetime+timeout
   
    numberOfWaiterG += 1
    printf "Waiting task number %g\r" , numberOfWaiterG
   
    //string WaitingTaskString = "waiterNum" + num2str(numberOfWaiterG)
    string WaitingTaskString = "waiter"
   
    if(numberOfWaiterG ==1)
        CtrlNamedBackground $WaitingTaskString,period=60,proc=waittaskUnified
        CtrlNamedBackground $WaitingTaskString, start
    endif
End

Function stopwaitUnified(ii)
    variable ii
   
    NVar numberOfWaiterG = root:numberOfWaiter
   
    wave endtimeW = root:endtimeW
    wave typeW = root:typeW
    wave/T TaskNameW = root:TaskNameW
   
    printf "Removed waiting Task number %g, with endtime %s, type %g and task %s\r" , (ii+1), (Secs2Time((endtimeW[ii]),3)), typeW[ii], TaskNameW[ii]
   
    //printf "%g. Alarm is on %s at %s\r", (ii+1),(Secs2Date((endtimeW[ii]),-1)), (Secs2Time((endtimeW[ii]),3))
   
    //string WaitingTaskString = "waiterNum" + num2str(numberOfWaiterG)
    string WaitingTaskString = "waiter"
   
    if(numberOfWaiterG <= 1)
   
        ctrlnamedbackground $WaitingTaskString,stop=1
        //killvariables root:endtime
        killwaves/Z endtimeW, typeW, TaskNameW
        numberOfWaiterG -= 1
        print "All Tasks done"
    else
        DeletePoints (ii), 1, endtimeW, typeW, TaskNameW
        numberOfWaiterG -= 1
    endif
   
End
 


//waitStatusPlusTimeLeft

Function waitStatusPlusTimeLeft()
   
   
    ctrlnamedbackground waiter,status
    if(numberbykey("RUN",S_info) != 0)
       
        NVar numberOfWaiterG = root:numberOfWaiter
       
        wave endtimeW = root:endtimeW
        wave typeW = root:typeW
        wave/T TaskNameW = root:TaskNameW
       
        printf "%g waiting task running\r" , (numberOfWaiterG)
        variable ii
        for(ii = 0; ii < numberOfWaiterG; ii += 1)
           
            variable Timeleft
           
            Timeleft = endtimeW[ii] - datetime
            printf "%g. Alarm is on %s at %s\r", (ii+1),(Secs2Date((endtimeW[ii]),-1)), (Secs2Time((endtimeW[ii]),3))
            printf "time for Alarm %g left %s\r",(ii+1),  (Secs2Time(Timeleft, 5 ))
        endfor
    else
        print "No timer running"
    endif  
   
    return numberbykey("RUN",S_info)
End

//unified wait task

Function waittaskUnified(s)
    STRUCT WMBackgroundStruct &s
   
    NVar numberOfWaiterG = root:numberOfWaiter
   
    wave endtimeW = root:endtimeW
    wave typeW = root:typeW
    wave/T TaskNameW = root:TaskNameW
   
    variable ii
   
    for(ii = 0; ii < numberOfWaiterG; ii += 1)
        string executeString
        sprintf executeString, "%s" ,TaskNameW[ii]
        if(datetime > endtimeW[ii])
            if(typeW[ii] == 1)
                ExecuteScriptText executeString
            else
                execute executeString
            endif
            stopwaitUnified(ii)
            //DeletePoints (ii), 1, endtimeW, typeW, TaskNameW
            //numberOfWaiterG -= 1
            //stopwait()
            //return 1
        endif
    endfor
    //print s
    return 0
End

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More