Search code examples
multithreadingaxaptadynamics-ax-2012x++dynamics-ax-2012-r3

Multi Threading in AX 2012 X++


Many of us are going through the optimization issues in AX 2012. In many of the cases optimization issues in process related code have no solution like we do have many other way around in reports.

Case: I had a case in which I have to perform a confirmation of multiple sales orders in AX 2012 on one button click. On confirmation of that sales order we need to perform some other 'Customized' process which is a bit lengthy process after following code practices and maximum optimizing the coding approach. So I have a question to how to handle this kind of scenario through multi threading


Solution

  • I have learnt about the feature of using multi threading using Thread class in Ax2012 then i tried to implement in following way.

    You first need to implement all your logic in Static method of a class. That static method should contain Thread Class as an parameter e.g

    public static void process(thread _thread)
    {
        FG_ConfirmationEngine   confirmationEngine = new FG_ConfirmationEngine();
        salesTable              salesTable;
        container               _con;;
        _con = _thread.getInputParm();
        info(conPeek(_thread.getInputParm(),1));
        salesTable = salesTable::find(conPeek(_thread.getInputParm(),1));
        confirmationEngine.parmSalesTable(salesTable);
        confirmationEngine.run();// in this method all of my confirmation pre and post logic exist
    }
    

    After creating that static method in a class you need to write the calling of that method. Note: You cannot send any Args, Object through Thread class. You can only send parameters in a form of container through thread.setInputParm() method like _thread.setInputParm([salestable.salesid]) method.

    Calling:

    salesline                   salesline;
    ExecutePermission           perm;
    Thread                      myThread;
    ttsBegin;
    
    perm = new ExecutePermission();
    
    if (!perm)
    return;
    
    perm.assert();
    
    while select salesid from salestable
        where salestable.FG_BookingReferenceID == "BRF-0001"
    {
        myThread = new Thread();
        myThread.setInputParm([salestable.SalesId]);
        if (myThread)
        {
            myThread.removeOnComplete(true);
            myThread.run(classnum(FG_ConfirmationEngine), staticMethodStr(FG_ConfirmationEngine,process));
        }
    }
    
    CodeAccessPermission::revertAssert();
    

    Hope it helps. Happy DAXing