Forums Register Login

infinite loop makes screen unable to draw

+Pie Number of slices to send: Send
hi guys.
i'm new to j2me .
do you have any idea what causes the problem with below code?
it's just example code in toolkit and i put in simple loop code there.
i thought that setCurrent adds SCREEN event and then that event is processed immediately (by other rendering thread?) if possible.
but program does not draw the screen when while statements are in there.
if I remove while statements, it works well and shows the "Test string" screen.
does it mean that draw is starting after executing startApp?
if then, how can I implement my event patching(something socket read) loop, and where is right place to put the loop?
of course, i can make event reading code with thread, but i guess event polling must be loop..
and what is excuting after startApp ends? i mean executing flow..just only event listener thread waits for event?
thanks in advance..and below is the code.
thank you =)

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMIDlet extends MIDlet implements CommandListener
{
private Command exitCommand;
private Display display;
public HelloMIDlet()
{
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 2);
}
public void startApp()
{
TextBox t = new TextBox("Hello MIDlet", "Test string", 256, 0);
t.addCommand(exitCommand);
t.setCommandListener(this);
display.setCurrent(t);
while (true)// what i added.
{
// i wanna check some conditions(ex. socket reading), and then process it repeatedly.
System.out.println(".");
}
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
+Pie Number of slices to send: Send
Hi,
I think u have to change ur way in calling a event dispatching..
Once the startApp() function is over the mobile application will wait for any commandAction(Command c , Displayable d) to executed by the user ...so if you want to do any operation like socket read...add a command button say "read" when the read is clicked put code something like this..
<h4><code>
public boolean ReadSocket() {
//write the code to read socket
}
public void commandAction(Command c, Displayable s) {
if (c == read) {
//clear the previous screen..
//call the ReadSocket function....
boolean done = ReadSocket();
// display the new screen.....
}
</code></h4>
regards
balaji

Originally posted by ht kim:
hi guys.
i'm new to j2me .
do you have any idea what causes the problem with below code?
it's just example code in toolkit and i put in simple loop code there......


+Pie Number of slices to send: Send
How about starting a new thread instead, to do you socket handling, or whatever? Then startApp() can finish and render the screen, and you can do whatever you want in your new thread (but don't suck up all the CPU time).
------------------
Richard Taylor
Author of Professional Java Mobile Programming
+Pie Number of slices to send: Send
very thanks for all of your help..
i wanna implement simple chat client for my study with J2ME.
so what i need is socket event listener.
i wanna patch the read event regardless of user action.
and whenever socket event has occured, the screen should be updated by the event.
so i can not use button event based reading.
so i think that should be some thread-based way as you said..
i also implement the socket event listening thread.
but socket event checking whether event has occured or not should be something like loop again, right? if then, the same problem will be occured.
or you mean that the new thread handles screen as well as socket event..? then is the thread new MIDlet?
thanks..
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 933 times.
Similar Threads
difficulty exiting a MIDlet on the WTK20 simulator
Something about current time
Error in Sun One Studio Mobile Edition
Where do the CLDC class files need to be placed?
easy fundamental oop problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:38:32.