Wednesday, May 4, 2016

AX7 - Code running on timer.

In previous versions of AX, we could use setTimeOut() method to execute a form method periodically (actually this method belongs to Object, so any class derived from Object could benefit from it). However, this method is deprecated in AX 7 and new setTimeoutEx() is introduced to replace it.
You may find setTimeOut() in source code, but it does nothing, and probably should cause BP error or warning.
I created a sample form to demonstrate how it works. It has only one Time control that is updated each 500 milliseconds. This form is quite similar to AX 2012 tutorial_Timer but it does not exist in AX 7 anymore.
TestTimerForm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Form]
public class TestTimer extends FormRun
{
    void run()
    {
        super();
        element.setTimeoutEx(formMethodStr(TestTimer, updateTime), conNull(), 500);
    }
 
    public void updateTime(AsyncTaskResult _result)
    {
        if (!element.closed()) //otherwise will be executed even after form close.
        {
            TimeControl.value(DateTimeUtil::getTimeNow(DateTimeUtil::getUserPreferredTimeZone()));
            element.setTimeoutEx(formMethodStr(TestTimer, updateTime), conNull(), 500);
        }
    }
}
When you call setTimeoutEx() method AX adds TimerControl control to a form and executes task asynchronously.
Please note two important things here:
  1. Method called by setTimeoutEx() should accept AsyncTaskResult as a parameter or run time exception will be thrown.
  2. updateTime() method checks if a form is not closed, otherwise AX will execute this code even after the form is closed.

That's it. the replacement for its 2012 version.

Thanks.