Tips & Tricks: multi-threading for long operations

by Giuseppe Lanzi on 03/15/2016

Managing long operations in your applications is a common event, especially in the field of business software. This can lead to a dilemma for the end user, who is not entirely sure how to behave.

In fact, when a long operation is launched, the application interface has to stop to wait for a response from the server, bringing up the classic please wait screen that we all know so well. This even happens with applications made with Instant Developer Foundation, but there is a very interesting solution: using Server Sessions.

Let’s imagine that we have an application procedure that takes anywhere from 5 to 60 seconds. In the example project, you can launch the method with the Long Method command, and you’ll see that the wait bar appears for a few dozen seconds. We don’t want the user to have this kind of experience, so let’s see what we can do about it.

First of all, we’ll use the startPhase and trackPhase methods in the long method, in order to tell the application how far along the operation has proceeded. In the example project code, a random number of steps is calculated, from 5 to 60, and each of them lasts 1 second.

Next, we decide to assign each user, recognized by their user name, a preset number of independent processes. In the example I have granted 3 for each user. When the operation begins, instead of launching it immediately, we’ll see if the user has a session available. Each session is identified by a name. In the example, it’s UsernameN, where N represents the process number.

To launch a process, meaning another application session, we use the startSession method, to which we send a query string that identifies the operation to be launched in the onCommand event. If there are no processes available, a message will be displayed that will ask the user to try again later.

At this point, we have to tell the user how many processes they have, and what their progress status is. To do so, we use the existsSession and sessionProgress methods, placing the results in an IMDB table to be shown on the screen. And we’re done.

What do you think? From now on, no more “please wait”.

What's the next trick you'd like to see?

  •    Web API: publish an app's data directly on a website
  •    Mobile: synchronizing multi-domain user data
  •    Integration of a graphic JavaScript component
Loading ... Loading ...

Leave a Comment

Previous post:

Next post: