• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

infinite loop makes screen unable to draw

 
ht kim
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
}
 
Balaji Loganathan
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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......

 
Richard Taylor
Author
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ht kim
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic