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.
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()|
|info('T1: Slow, blocking - was started first!');|
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();|
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.
If you continue, you will hit the “slow” method about 15 seconds later.
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).