AX 7. Using Timer to perform a periodic task.

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

[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.
Advertisements

2 thoughts on “AX 7. Using Timer to perform a periodic task.

  1. xiety August 25, 2016 / 7:25 am

    “otherwise AX will execute this code even after the form is closed”
    How long will this happen? Is there a potential memory leak? Can I stop the timer manually?

    • ievgensaxblog August 25, 2016 / 10:07 am

      It looks like It won’t stop while browser tab is open. So in the worst case you will have multiple form objects running infinitive loops in background but everything should be collected after tab close.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s