
Originally Posted by
Tiger79
I guess I could start off my thread right away in the startApp() method ? as in :
You probably never want to be calling a httpConnection thread directly as http connections can lock up and the thread can die. You need to have a main thread that runs and inserts requests for and probably isn't go.
Anyway here is something quick and dirty*
Code:
class MyMidlet extends MIDlet implements Runnable, CommandListener{
boolean isStarted = false;
Thread thread;
boolean sendDataWasPressed = false;
protected void startApp(){
if (isStarted == false) {
isStarted = true;
thread = new Thread(this);
thread.start();
}
else {
//startFromPaused();
}
}
public void commandAction(Command c, Displayable d) {
if(c == sendDataCmd){
sendDataWasPressed = true;
}
}
public void run() {
MyHttpConnection myHttpConnection = new MyHttpConnection();
//Create your form here and attach the commands to it
while(quitApplication == false){
if(myHttpConnection.hasTimedOut() == true){
myHttpConnection.setDead();
myHttpConnection = new MyHttpConnection();
}
if( sendDataWasPressed == true){
sendDataWasPressed = false
myHttpConnection.sendSomeData();
}
//feel free to do something with the data received from the connection here.
Thread.sleep(50);//sleep for a bit to give other threads a chance to run.
}
}
}
class MyHttpConnection implements Runnable{
Vector dataToSend = new Vector();
Thread currentThread;
boolean dead = false;
public MyHttpConnection(){
currentThread = new Thread(this);
currentThread.start();
}
public sendSomeData(){
dataToSend.addElement(...someData...);
}
public setDead(){
dead = true;
}
public void run(){
Connection conn = new Connection();
while(dead == false){
if(dataToSend.size() > 0){
// get the first peice of data to send
// and remove it from the queue of things to send.
}
}
//connection has died - try to do some cleanup and hope for the best.
}
}
*just like I like my coffee