Okay. Concept first.
Ivanov suggested that you would need to "get the event lock." When your code is executing in a thread that is Not The Main Event Thread (i.e., not the thread that is responding to user input, among other things), any changes you make to the UI (for instance, the text in the labels on the screen, or the length of a progress bar, etc.) will not show up. This is not an absolute - sometimes they might show up, but you cannot depend on behavior that has a "might show up" prospect. If you want the display (the UI) to reflect the modifications your code is making from within a background thread (which is any thread other than the Main Event Thread), you will need to do something special.
One approach is to use the "event lock" mechanism. This is in net.rim.device.api.system.Application, and can be acquired by the execution of the following code:
Code:
UiApplication.getUiApplication().getEventLock()
Check the method call in the RIM API description for the complete set of data regarding this call. Basically, it says you can use this to permit a "worker" thread (a background thread) to halt the Main Event Thread, manipulate the display, and then let it go (it means the changes that the worker thread makes to the UI will be implemented to the display).
Personally, I prefer the following approach. Instead of halting the event lock then modifying the dispaly and then releasing the event lock, I use the "invokeLater" method of the UiApplication to install a Runnable object that UiApplication will execute at some point during the Main Event Thread. Basically, the Main Event Thread is a big "while" loop, which checks for user input and other "events" that it might need to respond to. At some point before a pass through the loop is completed, the Main Event Thread looks for items placed in this "invokeLater" queue (I'm wild-azz guessing here, but it's pretty close to what happens) and executes their "run" method (since they all must be Runnables and therefore must have a run() method somewhere). This means that the code in the objects sitting in the "invokeLater" queue will get executed,
eventually, but at least the modifications to the UI will get executed from within the Main Event Thread.
So, here's an example.
Code:
class MyWorkerThread extends Thread
{
private LabelField _labelIWillChange;
public MyWorkerThread( LabelField inLabelToChange )
{
_labelIWillChange = inLabelToChange;
}
public void run()
{
// do your background task here
// when your task completes - or at its "I need to update" phase
UiApplication.getUiApplication().invokeLater( new UpdateRunnable( newText, _labelIWillChange ) );
}
}
class UpdateRunnable implements Runnable
{
private String _newText;
private LabelField _label;
public UpdateRunnable( String inNewText, LabelField inLabel )
{
_newText = inNewText;
_label = inLabel;
}
public void run()
{
_label.setText( _newText );
}
}
The above code snippet should work. It's not clear whether you also need to get the event lock as mentioned above, within the run() method of the UpdateRunnable. There are more compact ways of achieving the same thing (the UpdateRunnable class is not completely necessary, but it separates the issues so that you can see what's going on). Look through the RIM Development guides for more assistance.
Good luck, and may The Force be with you.
Cheers,
karl