-->

setTimeOut and local function

2019-07-20 07:27发布

问题:

I'm working on Ax 4.0

I'm trying to use the Object.setTimeOut method in a job with a local function, as stated in the msdn documentation :

static void setTimeOutJob()
{
    Object o = new Object();

    void printText()
    {
        ;
        info( "2 seconds has elapsed since the user did anything" );
    }
    ;
    // Set a Time Out with the idle flag set to false
    o.setTimeOut(identifierstr(printText), 2000, false);
}

But this simple job doesn't produce anything, so it seems I'm missing something here.

Has someone worked with this ?

回答1:

The setTimeout method does not work with a local function in a job.

For a working example have a look on the form tutorial_Timer instead.

Update:

The setTimeout method is a "magic" function, but it does not turn AX into a multithreading environment.

It only works while a Windows event loop is in action. In the AX context it means that a form is running and someone else is waiting for the form to complete. The sleep function does not meet the criteria.

Also the object must be "alive", calling a garbage collected object is no good!

Example (class based):

class SetTimeoutTest extends Object //Yes, extend or it will not compile
{
    str test;
}

public void new()
{
    super();
    test = "Hello";
}

public str test()
{
    return test;
}

protected void timedOut()
{;
    test = "2 seconds has elapsed since the user did anything";
    info(test);
}

static void main(Args args)
{
    SetTimeoutTest t = new SetTimeoutTest();
    FormRun fr;
    ;
    t.setTimeOut(methodStr(SetTimeoutTest,timedOut), 2000, false);
    //sleep(4000); //Does not work
    fr = ClassFactory::formRunClassOnClient(new Args(formstr(CustGroup))); //Could be any form
    fr.init();
    fr.run();
    fr.wait(); //Otherwise the t object runs out of scope
    info(t.test());
}


回答2:

I just don't think it works with jobs. I've used it on forms where the method is on the element level, and have done element.setTimeout and it works fine.



标签: x++ axapta