web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

X++ Async Functions

(0) ShareShare
ReportReport
Posted on by 211

Hey,

I need a help about async methods in x .


My requirements are, i want to do some functions after insert a table but i dont want to wait user or whoever insert for my functions.

I found some methods about Thread here is my code like : 

// you can think this place after insert purchLine.

Thread thread;

thread = new Thread();
t.setInputParm([purchLine.RecId]);
if(thread)
{
    t.removeOnComplate(true);
    t.run(classNum(MyStaticClass),staticMethodStr(MyStaticClass,proces));
}

and 

public static class MyStaticClass
{
    public static void proces(Thread _thread)
    {
        MyEntegration entegration = MyEntegration::construct(curExt());
        entegration.informEntegration(PurchLine::findRecId(conPeek(_thread.getInputParm(),1)); // i am preparing information about purchline and sending to web api and api puts them on message queue
    }
}

so my first question is there any way to use thread without static classes methods and can you share with me any examples how can i perform this logic with runbasebatch ?

  • Ferhat.S Profile Picture
    211 on at
    RE: X++ Async Functions

    Thank you so much Martin finaly achieve to send data to api.

    see you on next questions :)

    Have a nice day!

  • Martin Dráb Profile Picture
    225,155 Moderator on at
    RE: X++ Async Functions

    SysOperation framework is the supported solution for this scenario and it's more reliable, because it doesn't depend on user's session (if you use ReliableAsynchronous or ScheduledBatch modes).

    Even if you had a scenario when using threads directly would make sense, you shouldn't just go and create any number of threads. You should use a thread pool instead, and in fact, you should avoid dealing with threads directly and rather use the Task Parallel Library. There is some support F&O for that (e.g. runAsync()), but you should generally avoid it too and simply use the SysOperation framework.

  • Martin Dráb Profile Picture
    225,155 Moderator on at
    RE: X++ Async Functions

    I'm sorry, but I don't understand what you mean by "How can i pass purchLine this proces method or any information about certain line to there". Please elaborate. If you mean that you want to pass some parameters to proces(), use the solution you've rejected - a data contract class. It's the purpose of data contract classes.

    Regarding your problem with parmShowDialog(false), it's possible that "true" is stored in SysLastValue and it overwrittes the value set in main().

  • Ferhat.S Profile Picture
    211 on at
    RE: X++ Async Functions
    [quote]

    Don't use Thread

    A business event may be the right approch. Or you can utilize using SysOperation framework with one of the asynchronous modes.

    Or you can change the design by just writing a record to a process queue on insert, and having a separate process for processing the queue. That's what business events do too.

    [/quote]

    And can you tell me about more why i shouldnt use Thread ?

  • Ferhat.S Profile Picture
    211 on at
    RE: X++ Async Functions

    Hey Martin,

    Sory for late answer i was searchin business event sysoperation framework. I decide do with operation framework as i search more i write a code like that but have some problem with it

    I create first SysOperationService

    class MyAsyncSysOperationService extends SysOperationServiceBase
    {
        public void proces()
        {
    
           // doing my stuff here
            
        }
    
    }

    But examples shows me datacontract class for parameters but i dont want to use. How can i pass purchLine this proces method or any information about certain line to there.

    also antoher problem is i wrote controller class which is :

    class MyAsyncSysOperationController extends SysOperationServiceController
    {
        protected void new()
        {
            super(classStr(MyAsyncSysOperationService),methodStr(MyAsyncSysOperationService,proces),SysOperationExecutionMode::Asynchronous);
        }
    
        public static MyAsyncSysOperationController construct(SysOperationExecutionMode _executionMode = SysOperationExecutionMode::Asynchronous)
        {
            MyAsyncSysOperationController controller;
            controller = new MyAsyncSysOperationController();
            controller.parmExecutionMode(_executionMode);
            return controller;
        }
    
        public static void main(Args _args)
        {
            MyAsyncSysOperationController controller;
            controller = MyAsyncSysOperationController::construct();
            controller.parmArgs(_args);
            //controller.parmShowDialog(false); note: this isnt worked me still shows dialog i had to overwrite this below.
            controller.startOperation();
        }
    
        public boolean parmShowDialog(boolean _showDialog = showDialog)
        {
            return false;
        }
    
    }

    and as i marked at code parmShowDialog(false) parameter is not worked on my test cases so i had to overwrite it is that right aproch ?

    Thanks for help.

  • Verified answer
    Martin Dráb Profile Picture
    225,155 Moderator on at
    RE: D365 X++ Async Functions

    Don't use Thread

    A business event may be the right approach. Or you can utilize using SysOperation framework with one of the asynchronous modes.

    Or you can change the design by just writing a record to a process queue on insert, and having a separate process for processing the queue. That's what business events do too.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

Product updates

Dynamics 365 release plans