multithread and http

Hi,

am facing a strange problem, i have done one simple application to upload a file on to server. I used one main thread set infinite to do the animation part and created an object of other class where i have written code for the http connection part in a thread. problem is the file is getting uploaded but am not able to go to any other state. the mdilet will be in an infinite mode.

Regds,

j2me apps developer

[438 byte] By [j2me@coma] at [2007-11-27 11:02:11]
# 1

Ask your self this: why is this happening, and what can I do about it to change that...

There is not much more to say without more information...

deepspacea at 2007-7-29 12:41:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

well true,

let me tell u again.

i have created thread am made it as infinit like

class a

public void run()

{

while(true)

{

different states for animations.

}

}

created one more object of "x" class in class "a".

x class contains methods to upload or download the file to do that i have created one more thred.

public void run()

{

uploaddownload();

}

public void upload downloaddownload()

{

code to send and recieve data.

end line will be chanhging the state of main thread.. assume fromprocessing to completed.

}

when i sratr the thread of class "x" assume i am in processing state of main thread, in the last line of uploaddownload method am changing the state processing to state completed. but the state change is not happening.. it is still in state processing.

j2me@coma at 2007-7-29 12:41:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

use break; when you have reached the final state! somthing like

if(state1)

{

break;

}

jonney69a at 2007-7-29 12:41:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

let me tell u in detail,

//main class

public void run()

{

while(true)

{

switch(state)

{

case pregress:

if(uploadsuccess)

{

state = completed;

}

else

{

// animation

}

repaint();

break;

case completed:

repaint();

break;

}

}

}

///////// other class

if(keypressed == send)

{

new Thread(this).start();

}

public void run()

{

upload();

}

public void upload()

{

//// http connection code

mainclass.uploadsuccess = true;

}

--

Even when i set the uploadsuccess = true; this is not entering the if condition in main thread. uploading of file is happening but the change in the states is not working. Emulator testing is perfect. On nokia 3230 the problem.

j2me@coma at 2007-7-29 12:41:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

A break will in this case only exit the switch statement, and not the while loop (in any case, a break belongs at the end of every case!).

You could do a return instead. It will just exit the run method and end the Thread.

deepspacea at 2007-7-29 12:41:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6

well i tried that. no success. even problem is like am assigining some values to some temporory variable and trying to change the value when the each step proceeds in the second thread and tried painting it on main class. strange is that there is no change seen on screen. but the file is been uploaded when i checked it on server.

j2me@coma at 2007-7-29 12:41:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7

Well, then your problem is elsewhere. just debug your application logically...

deepspacea at 2007-7-29 12:41:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8

I think you should do it like thiswhile(!mainclass.uploadsuccess) {...}

to get it worked. but I strongly oppose this kind of coding idea of kind of looping thru codes until communication success.

find_suvro@SDNa at 2007-7-29 12:41:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9

From the sounds of it, you actually want a finite loop; a animation that runs from the beginning of the upload to the end.

Your upload animation canvas should have a methods to start and stop the animation and/or override hide and show notify. There is no reason for the thread to be busy when the animation is not displayed.

Here is how I would approach the animation and upload

Psuedocode:

SomeClass {

public void upload()

{

animationCanvas.startAnimation();

display.setCurrent(animationCanvas);

try

{

//do upload

}

finally

{

animationCanvas.stopAnimation();

display.setCurrent(someScreen);

}

}

}

AnimationCanvas extends Canvas implements Runnable

{

Thread thread = null;

paint(Graphics g) {};

public void startAnimation()

{

if ( thread == null )

((thread = new Thread(this)).start();

}

public void stopAnimation()

{

thread = null;

}

public void run()

{

while ( thread != null )

{

//animate, repaint, service repaints if needed, sleep

}

}

}

The finally is important as it ensures that the animation stops.

You could also explore the use of a TimerTask. serviceRepaints will be needed on some devices to force the graphics to repaint.

rashidmayesa at 2007-7-29 12:41:39 > top of Java-index,Java Mobility Forums,Java ME Technologies...