BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 02-21-2007, 01:39 PM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2006
Model: 8310
Carrier: AT&T (formerly Cingular)
Posts: 47
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Communication between threads

Please Login to Remove!

I'm still learning how to program for my Blackberry so my first program is a simple one that will retrieve an image file from a server using a TCP/IP connection.

I have two threads in my program. One is the main thread that constructs the UI and then starts the second thread which handles the retrieval of image data over TCP/IP.

I can successfully retrieve the data from the server into a byte array but I'm stumped as to how to get the data from my "network" thread to my main thread. I haven't had much luck finding tutorials that explained this in depth. The one bit of sample code I did find doesn't want to compile, but it is supposed to update the _content field's text that is on the MainScreen. From what I've read I'm supposed to be able to call the updateContent routine which will in turn modify the control on the main screen. I tried calling a routine in my main thread from within the updateContent routine but it doesn't work. It's as if the routines and functions within my main thread are private. However, I made the class public. I get a compiler error of "cannot find symbol"



Code:
private void updateContent(final String text) {
    UiApplication.getUiApplication().invokeLater(new Runnable()
    {
        public void run()
        {
            _content.setText(text);
        }
    });
}
I'm lost with this. Can anyone point me toward a decent tutorial with samples or post a snippet here?

Last edited by forcedfx : 02-21-2007 at 01:48 PM.
Offline  
Old 02-21-2007, 01:48 PM   #2 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey ForcedFix
BlackBerry - BlackBerry | Wireless Handheld Devices, Software & Services from Research In Motion (RIM) should point you in the right direction. I'm actually hammering against this problem right now as well. The issue is there is only one thread holding the application (which can be accessed through Thread.currentThread(); ) If you try and run a parrallel thread that access the current UiApplication, you'll get an IllegalThreadStateException. You could try and join the threads, and if I figure out how to do that myself I'll let you know.
Offline  
Old 02-21-2007, 02:05 PM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Nov 2006
Model: 8310
Carrier: AT&T (formerly Cingular)
Posts: 47
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks bemshaswing. I think the link you gave me might have gotten me going in the right direction. I had my MainScreen and Connection threads outside of my project's main class, which (obviously) made my LabelFields "invisible" to any other class. I'm now able to change the text of a text field from my secondary thread.
Offline  
Old 02-22-2007, 04:08 AM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2007
Model: 8800
Carrier: Orange
Posts: 181
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

What you want todo is set a callback on your main thread. Create an interface Callback or something with a method setText(String t) or something. Make your main thread implement that interface.

On the comms thread have a method setCallback(Callback c) and after you create the comms object call comms.setCallback(mainThread). Then when the comms thread is done fetching the data get it to call c.setText(text).

Hope that helps!
Offline  
Old 02-22-2007, 02:42 PM   #5 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey bdowling,
I know you can get it at runtime, but how do you get access to the main thread ahead of time so you can have it implement the interface?
Offline  
Old 02-23-2007, 06:28 AM   #6 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2007
Model: 8800
Carrier: Orange
Posts: 181
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

You have to make the main thread implement the listener interace (in code)
Offline  
Old 02-23-2007, 10:59 AM   #7 (permalink)
New Member
 
Join Date: Feb 2007
Model: 7290
Carrier: TMobile
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or you could make the thread class an inner non-static class (defined inside your Screen class without a static keyword. Because it will be non-static, it wil have a reference to the Screen object and to all of its public members (like that text field).

This aproach kinda breaks encapsulation, but for a simple task such as this i think it's better than making a callback.

But if you are trying to write a more elaborate worker thread, you should use the callback approach because it's more maintainable and reusable.

Also, sometimes you have to synchronize with the event lock when working with GUI methods from a non-event thread.

synchronised(UiApplication.getEventLock())
{
... your code ...
}
Offline  
Old 02-23-2007, 11:24 AM   #8 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Dane, I think I get where you're going with this.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Samsung INR18650-30Q 3000mAh Unprotected Button Top 18650 Battery 2pcs
$14.38
Samsung INR18650-30Q 3000mAh Unprotected Button Top 18650 Battery 2pcs pictureINR18650-30Q 3000mah 20A Power Li-ion Battery for Samsung 2pcs
$13.19
INR18650-30Q 3000mah 20A Power Li-ion Battery for Samsung 2pcs pictureLogic Board 50UF2 LJ41-C8382A LJ92-01701A Samsung TV PN50C550G1F PN50C550G1FXZA
$18.0
Logic Board 50UF2 LJ41-C8382A LJ92-01701A Samsung TV PN50C550G1F PN50C550G1FXZA picturePower Main Board BN44-00329A BN44-00330A Samsung TV PN50C550G1F PN50C550G1FXZA
$18.0
Power Main Board BN44-00329A BN44-00330A Samsung TV PN50C550G1F PN50C550G1FXZA pictureSpeaker Set BN96-21669C for Samsung TV Model UN50H5203AFXZA
$12.0
Speaker Set BN96-21669C for Samsung TV Model UN50H5203AFXZA  picture






Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.