Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 157
Registered: ‎07-09-2009
My Device: Not Specified

How to implement Multi thread?

Hi Friends,

 

I am creating an application which is related to web server upload and processing.

 

I need to show the progress bar (with exact percentage completed) while posting/reading content to/from server.

 

I tried by some codes but I unable to show the percentage completed. While post thread is running it stops the percentage calculation thread or related threads.

 

I think we need to multi thread concept for this. But, I cannot achive on this.

 

Am I going in correct way?

Can any one help me on this?

How to use multi thread in my application?

 

Thanks in advance for your responses.

Developer
Posts: 19,624
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to implement Multi thread?

Developer
Posts: 157
Registered: ‎07-09-2009
My Device: Not Specified

Re: How to implement Multi thread?

Thanks for the response Peter.

 

I tried with that code. I have  found following issues:

 

1. Post thread not calling until completion of progress bar. It is not actual progress bar and it not exact. It just dummy progress bar. It simply waiting for some minutes and after that only it starts to post the contents.

2. If the server response is slow then it showing the text  "Finished" for few minutes. I have checked it thoroughly with all cases which I know.

3. What is the two variables "updateInterval" and "stallTime'?. I think the progress depends only on these two variables not depends on the length of the content and server response.

 

Any suggestion from your end?

Developer
Posts: 213
Registered: ‎07-18-2008
My Device: Not Specified

Re: How to implement Multi thread?

you might also find the example in this link interesting:

 

http://www.naviina.eu/wp/blackberry/loading-class-for-blackberry/#more-607

--------------------------------------------------
problemSolved() ? kudosPlease():kudosPlease();
Developer
Posts: 19,624
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to implement Multi thread?

The sample code is just that, it show show you can have an active popup screen, showing status of the long running Thread.  It demonstrates one reasonably robust approach to doing the multi-Threading that you asked for. 

 

But it is just a sample and it does not have any networking, it simulates this with a simple sleep loop Thread. 

 

You can add in the networking code yourself.  And you can tailor this code to match your requirements, for example you could determine how much data there is left to get and so set the exact percentage by calling

observerStatusUpdate(percentageFinished, "Processing: " + Integer.toString(percentageFinished));
more often.

 

I'm sorry this is not a simple bolt in to your application, but that is typically the way with samples!

 

 

Developer
Posts: 157
Registered: ‎07-09-2009
My Device: Not Specified

Re: How to implement Multi thread?

Hey yosoh,

 

Thanks for your response.

 

I want to show the progress bar not just loading popup.

 

Developer
Posts: 213
Registered: ‎07-18-2008
My Device: Not Specified

Re: How to implement Multi thread?

[ Edited ]

I saw this code longback in forums and use it for myself. Thanks to the original poster of this code:

 

 

 

/**
* Defines ProgressBar object. Creates popup screen with title, and perpetually updating
* progress gauge. Instantiate and run as thread to start progress update. Call
* remove() method when finished to remove popup screen and shutdown thread.
*/


import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.DialogFieldManager;
import net.rim.device.api.ui.container.PopupScreen;

public class ProgressBar extends Thread {

private int maximum, timeout;

private boolean useful;

private PopupScreen popup;

private GaugeField gaugeField;

/**
* Object constructor
*
* @param title
* Text to display on popup area
* @param maximum
* Range / width of the gauge field of progress bar
* @param timeout
* Milliseconds to pause between updates to progress bar
* @see GaugeField
* @see Thread
* @see PopupScreen
*/

public ProgressBar(String title, int maximum, int timeout) {
this.maximum = maximum;
this.timeout = timeout;

DialogFieldManager manager = new DialogFieldManager();

popup = new PopupScreen(manager);
gaugeField = new GaugeField(null, 1, maximum, 1, GaugeField.NO_TEXT);

manager.addCustomField(new LabelField(title));
manager.addCustomField(gaugeField);
}

/**
* run() method for starting thread
*/

public void run() {
useful = true;

UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(popup);
}
});

int iterations = 0;

while (useful) {
try {
Thread.sleep(timeout);
} catch (Exception e) {
}

if (++iterations > maximum)
iterations = 1;

gaugeField.setValue(iterations);
}

if (popup.isDisplayed()) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().popScreen(popup);
}
});
}
}

/**
* Method to shutdown the thread and remove the popup screen
*
*/

public synchronized void remove() {
useful = false;
}
}

 

 

 

 

 

--------------------------------------------------
problemSolved() ? kudosPlease():kudosPlease();