BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 12-03-2007, 03:49 PM   #1
predator
Knows Where the Search Button Is
 
Join Date: Nov 2007
Model: 8700c
PIN: N/A
Carrier: AT&T
Posts: 32
Unhappy Starting a thread with .start();

Please Login to Remove!

My application was working perfectly and so was my networking thread until i did some updates to my code. Nothing has been modified in the networking thread and it's still being called the same way as well. The problem i'm having now is when i start the networking thread

Networking thread snippet.
Code:
public class NewThread extends Thread
{
    public void run()
    {
         try
         {
              Dialog.alert("in try");
                                                
              conn = (HttpConnection)Connector.open(URL);
              conn.setRequestMethod(HttpConnection.GET);
              // ... more code
Call snippet.
Code:
NewThread nt = new NewThread();
                nt.start();
                //nt.run();

                try 
                {
                    Dialog.alert("try1");
                    while (myWait)
                    {
                        Thread.sleep(1000);
                    }
                }
                catch (InterruptedException e) 
                {
                    Dialog.alert("catch1");
                }
                Dialog.alert("done try1");
If i use nt.start(); the application freezes (since it's in the while(myWait) block). It never starts the run() block (i never get the dialog box that i should, once it enters the try block) so myWait is never set to false.

If i use nt.run(); instead, I get the "blocking operation not permitted on event dispatch thread" error.

I know i have to use nt.start() and i was able to before. I see no reason for it not to work now.. Any ideas?
Offline  
Old 12-03-2007, 04:27 PM   #2
koedur
Knows Where the Search Button Is
 
Join Date: May 2007
Model: 8800
PIN: N/A
Carrier: Rogers
Posts: 17
Default

Actually, your class NewThread probably runs. I think what's happening here is that the very first line of code in the run() method is throwing an exception since it's a GUI operation (you're trying to open a dialog).

You will need to run any gui operations on the event dispatch thread or hold the event lock (see DB-00134). If you've done any gui work with swt or swing this concept should be familiar.

The reason why you're getting the error msg "blocking operation not permitted on event dispatch thread" is exactly what it says. When you call the run() method, you're not creating a new thread at all. It's just another method call. In this case, the caller of run() is just the main event dispatch thread.

The piece of code "Connector.open(URL)" is a block operation (the javadocs should mention that it is). If you want to open a socket, this has to be done on a background thread. Your NewThread class is fine but like I said about, you will need to execute all gui operations on the event dispatch thread.
Offline  
Old 12-03-2007, 04:49 PM   #3
predator
Knows Where the Search Button Is
 
Join Date: Nov 2007
Model: 8700c
PIN: N/A
Carrier: AT&T
Posts: 32
Default

Thanks for the reply I really appreciate it.

Unfortunately, though, that didn't solve the problem but your post was very informative. I removed all the dialogs and my application still freezes. If i remove the network call, the application runs. For my application, i need to pull some files using the networking thread. It's still an infinite loop at "while (myWait)".

The only reason i added the dialogs there was to see if the run() method was executing at all. I'm limited to doing my testing on the device itself due to firewall issues ( i think) that cause my MDS to not work with the simulator.
Offline  
Old 12-03-2007, 06:24 PM   #4
richard.puckett
Talking BlackBerry Encyclopedia
 
richard.puckett's Avatar
 
Join Date: Oct 2007
Location: Seattle, WA
Model: 9020
PIN: N/A
Carrier: T-Mobile
Posts: 212
Default

Maybe I'm not seeing something, but why *would* your myWait loop ever get interrupted? How do you know when the networking thread completes?

Re MDS: Have you tried restarting your simulator after you load a new cod? I've noticed that my simulator will not communicate with MDS after I've reloaded a cod from disk. Have to restart the simulator and then it works. Can make for a frustrating debug cycle (which is why I've gotten more into symbolic debugging through Eclipse rather than printing debug messages on the device).
Offline  
Old 12-03-2007, 07:28 PM   #5
predator
Knows Where the Search Button Is
 
Join Date: Nov 2007
Model: 8700c
PIN: N/A
Carrier: AT&T
Posts: 32
Default

I'll try restarting the simulator when I go in to work tomorrow Thanks for that tip. Here's hoping it works, hehe.

As for the myWait loop, what i did was i set the boolean myWait to true, start my networking thread, and have my application check that boolean variable every one second. at the end of the networking thread, i set myWait to false, so the application breaks out of the loop and continues from that point. For my application, I need it to execute some code, then wait for the networking thread to get done (pull a file, format the received data) and then continue from that point.

Last edited by predator; 12-03-2007 at 07:48 PM..
Offline  
Closed Thread



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


Digital Thermometer Dual Channel Input Thermocouple K J T E R N C/F Selection US picture

Digital Thermometer Dual Channel Input Thermocouple K J T E R N C/F Selection US

$25.99



 Digital Thermocouple Thermometer Dual Channel 2*K-Type Temperature Meter picture

Digital Thermocouple Thermometer Dual Channel 2*K-Type Temperature Meter

$22.99



Cole-Parmer Digi-Sense Thermocouple Thermometer Model 8528-20 picture

Cole-Parmer Digi-Sense Thermocouple Thermometer Model 8528-20

$49.99



K-Type Thermocouple Probe Digital Thermometer Steel Sensor Spiral Cable  picture

K-Type Thermocouple Probe Digital Thermometer Steel Sensor Spiral Cable

$11.49



Thermo Electric  G/G-20-K Thermocouple Wire 900’ Roll -NOS - Make Offer picture

Thermo Electric G/G-20-K Thermocouple Wire 900’ Roll -NOS - Make Offer

$299.95



K Type Temperature Sensor Probe 1.5M Cable 4mmx100mm Probe Thermocouple picture

K Type Temperature Sensor Probe 1.5M Cable 4mmx100mm Probe Thermocouple

$17.77







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