Threading Basics (BlackBerry)

 
Jared Wiltshire 5th September 2011

IntroApp Icon

Threading on any device allows code to be executed in the background while the User Interface continues to show and update.

Without code execution in the background; the user interface would freeze leaving the user clueless as to what is happening.

This article will explain how it is possible to run code in the background whilst operations continue in the UI thread. I will also explain how to show a loading indicator to the user (to lock the screen but keep the user aware) while a background process is taking place.


Basic Code

The basic principal of writing threaded code in BlackBerry SDK is to implement the Runnable class on a class that you want to run in the background.

[Code sample – Class implementing Runnable]
public class cThreadingClass implements Runnable


Within this class that implements the Runnable class, you need to add the Run() method.

[Code sample – Implementing Runnable Run() method]
public void run ()
{
     // background code here
}


You pass an instance of your class to the Thread constructor before calling its start method:

[Code sample – Starting a new thread]
cThreadingClass oThreadingClass = new cThreadingClass()
new Thread(oThreadingClass).start();


This calls the run() method on the cThreadingClass class.

If you have an existing class that you wish to call methods of in the background, it might be an idea creating wrapper classes for each of the methods that you want to call. For example;

You have a class cBackgroundMethods that contains the methods GetImagesFromServer() and GetTextFromServer()

You could create wrapper classes for both methods called cBackgroundMethodsGetImagesFromServer and cBackgroundMethodsGetTextFromServer

Both of these classes will implement the Run() method,

cBackgroundMethodsGetImagesFromServer.Run() calls cBackgroundMethods.GetImagesFromServer()
cBackgroundMethodsGetTextFromServer.Run() calls cBackgroundMethods.GetTextFromServer()
(methods are expressed here in a static format for readability purposes)

Both of these methods can now be called as new background operations using the new Thread([Your class here]).start(); method.

While the code is executing in the background, the UI thread remains responsive and code will continue to execute after this line of code is called (non-blocking).


Loading indicator

Having a operations run in the background is important to keep the UI available to the user, however what if you want to block any UI interaction but also want the user to be notified of a background operation being carried out at the same time?

This is where a loading indicator is best suited. Using a popup window, display text and a loading indicator image, you can achieve a professional looking “Please Wait” style popup for the user to view to notify them of background activity taking place.

This is particularly useful when submitting data to a server over the air where the user should wait for a response before any further action can be carried out.

The first thing to do is create a void type method within your UI class, for example public void myThreadedMethod()

The first line of the method should define a new Thread object which you will define in-line.

[Code sample – In line thread]
Thread oLoadThread = new Thread()
{
     // … In-line code declaration here
}


Next you should define three sections of code:
  1. UI actions during the background operation, e.g. Showing the popup screen
  2. Background operation e.g. Calling the background method
  3. UI actions after the background operation has completed, e.g. Hide the popup screen, move the user on to another form


The two actions during and after on the UI thread need to be called within the UiApplication.getUiApplication().invokeLater() method.

InvokeLater adds a request onto the event stack to allow background threads to request changes to the UI.

A basic implementation of this would be as follows:

[Code sample – In line thread code]
// UI Actions during the background operation (show waiting message)
UiApplication.getApplication().invokeLater( new Runnable()
{

    public void
run ()
    {
         UiApplication.getUiApplication().pushScreen(oFrmPleaseWait);
    }

});


// Background operation
cBackgroundMethodsGetImagesFromServer.Run();

// UI Actions after the background operation has completed
//(hide waiting message)
UiApplication.getApplication().invokeLater( new Runnable()
{

    public void
run ()
    {
         UiApplication.getUiApplication().popScreen(oFrmPleaseWait);
    }

});


In this example, oFormPleaseWait is a PopupScreen type object.

Putting all of this code into the myThreadedMethod() method we mentioned earlier and wrapping it inside a the Thread oLoadThread = new Thread() { // …. } in-line method declaration means we can call the myThreadedMethod() method at any point in our code and it will perform a background operation, show a loading indicator to the user while they wait for the background operation to complete, and then hide the loading indicator once finished.
 

Add Comment

 
Name:
Email:
This email address will not be shown on the website and will not be distributed to any other parties. By submitting this email address you agree to receive an email from Eigo in the future, that email will include an option to unsubscribe from future emails.
Comment:
(HTML allowed)
Website: (Optional)
A link to show beneath your comment.
Security Check: