setTimeOut and local function

和自甴很熟 提交于 2020-01-05 02:52:16

问题


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.



来源:https://stackoverflow.com/questions/11359271/settimeout-and-local-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!