Simple Threading in AX 2012

In this post I want to show, how easily you can utilize the Thread class in AX 2012.

Simple Thread example

First we want to setup a new class.

class MyDemo
{
}
view raw thread_class hosted with ❤ by GitHub

Next we want to add two static methods, one “slow” method which will use a sleep to simulate a lengthy runtime and one “fast” method.

public static void fast()
{
info('T2: Fast - started second!');
}
public static void slow()
{
sleep(15000);
info('T1: Slow, blocking - was started first!');
}
view raw thread_methods hosted with ❤ by GitHub

The idea here is that we invoke the “slow” method at first and the “fast” method later on. Todo so, we setup a new job.

static void ThreadDemo(Args _args)
{
Thread firstT = new Thread(),
secondT = new Thread();
firstT.run(classnum(MyDemo), identifierstr(slow));
secondT.run(classnum(MyDemo), identifierstr(fast));
pause;
}
view raw thread_job hosted with ❤ by GitHub

First we create two new Thread objects, then we call the run method of it and pass the class plus the static method we want to execute. Like said, first the slow, then the slow.

Before we run the job, set break points to the both info(), as they will not pop up when running the job, because they run in a new worker thread.

First thing we will notice, is that we will hit the “fast” method first.

AX2012 Threading Fast Method

If you continue, you will hit the “slow” method about 15 seconds later.

AX2012 Threading Slow Method
As you can see, working with threads in AX 2012 is very easy. Instead of waitin till the “slow” method is finished, the “fast” method gets invoked immediately after the “slow” fires.

If you need further information, I found this article on the Dynamics Community very helpful. It provides a little bit more inside into threads (also give the linked post in that article a chance).

You may also like...