Using Timer

Using Timer

If you want perform a periodic task you can use batch processing, but there is another option to perform local fast periodic tasks - timers.

You can call the setTimeOut function in forms or client-side classes like this:

this.setTimeOut("updateRealTime", 1000, FALSE);

where

  • "updateRealTime" - Name of method to call.
  • 1000 - wait time in milliseconds
  • true - means that time should only be measured while the program is idle.

This example is taken from tutorial_Timer form

Lets see at the updateRealTime source:

void updateRealTime()
  {
    _realTime.value(timeNow());
    _realTimer = this.setTimeOut("updateRealTime", 1000, FALSE);
  }

it performes some useful action (realTime.value(timeNow());) and then sets the timeout again, so the action will be performed every second.

Another pattern of using timers is to perform something in the background or as an interruptable task. The clearest example is SysAotFind form which performs a search through the AOT.

The pattern is as follows:

  • declare a "stop flag" in your object
  • divide your task into small iterative steps
  • execute each step in the timeout function
  • set the timeout again at the end of the timeout function, if the stop flag is not set
  • when user presses the stop button, set the stop flag to true. Execution will stop at the end of the next cycle.

see \Forms\SysAotFind\Methods\nextTreeNode for example

posted @ 2012-02-15 17:49  perock  阅读(217)  评论(0编辑  收藏  举报