Omnis Technical Note TNWE0014 May 2007

Asynchronous Programming in Omnis

For Omnis Studio 4.2 or after (Pro Edition, plus the Omnis Web Services component)
By Götz Krija

There are two programming models with which you can implement functionality - synchronous and asynchronous programming. In synchronous programming, each function or method call has to be completed before the next one can start. This is the default behavior in Omnis. The disadvantage of this model is that the application process may have to wait for user input, such as a click on a button, before it can proceed with other tasks. It could be more efficient to run different processes alongside each other. A remedy can be found with the asynchronous programming model. In this case, a function or method can be started in a separate thread and this can be processed in parallel with another function in its own thread. This can provide a faster and more efficient application, since method execution is not halted when user input is required, such as when a message box is presented or a long report is being generated.

Imagine a situation where your application needs to perform a data import but can perform some other function at the same time as the import. In a synchronous execution the data import would have to be completed first and the program would be blocked. In an asynchronous execution the data import can be processed in the background while other code is executed.

To try the following example, you will need the Omnis Studio Pro Edition and the Omnis for Web Services component. Let's take a simple example: In a loop a variable is totalised. At the same time an OK message is presented to the user with the hint that they can continue their work although a calculation is executed in the background.

Add a new remote task called 'rTest' to your library and create a new method called $backgroundMethod(). Right-click on this method and mark it as a static Web Service, as shown.

Static Web Service method

Then add the code as follows:

For counter from 1 to 1000000 step 1
  Calculate counter2 as counter+1
End For
OK message {Background process finished!}

Now create a new window with a button and insert the following code in the $event() method of the button:

Start server
Do async method rTest/$backgroundMethod
OK message {Counter method is continuing in the background!}

To test this functionality, open an instance of the window and click on the button. At first you will be presented with the OK message telling you that the method will continue. When you confirm this by clicking OK, the background process will continue, but when it has finished another OK message will be displayed.

When you are executing methods asynchronously, you must make sure you are programming in a "thread safe" environment, that is, you should not use any global variables or commands that change the global Omnis flag. You will get unexpected or incorrect results if you use commands that change the flag while running methods asynchronously. When you want to return to synchronous execution (the default execution), use the Stop server command.