Archived

This topic is now archived and is closed to further replies.

someone2

How to kill the thread?

Recommended Posts

someone2    116
Hi, I am having a little weird problem! I am following the tutorial of MIDP 1.0 found here in gamedev.net The problem is.. how can i exit the program? returning from run() simply doesn''t make the program close! What is the way to exit the program? Thank you very much in advance...

Share this post


Link to post
Share on other sites
Zahlman    1682
Calling notifyDestroyed() should do it.

You may want to implement destroyApp() in such a way that it kills your thread. One way to do this:

public class myTest extends Midlet implements Runnable {

private boolean running;
private Thread t;
public void startApp() {
running = true;
t = new Thread(this);
t.start(); // hold on to a reference

}

public void run() {
while(running) {
// do something

}
}

public void destroyApp() {
running = false; // allow thread to break out of loop

t.join(); // wait until that thread finishes its last iteration

// any other cleanup here

}
}

Share this post


Link to post
Share on other sites