Thread: Timer issues
View Single Post
Old 11-12-2014, 12:52 AM   #6
ChronosDragon
Junior Member
ChronosDragon began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Oct 2014
Location: Seattle area
Device: Kindle 4 Non-Touch
Just as a follow up, I came back to this after leaving it for a month or so and got it working a different way. I never got the timer working, but I was able to achieve the same result with a thread:

Spoiler:

Code:
class WorkerThread extends Thread {
    private Main main;
    public volatile boolean shouldRun = true;
    public volatile boolean shouldExit = false;
    public WorkerThread(Main main) {
        this.main = main;
    }

    public void run() {
        for (;;) {
            if (shouldExit) { return; }
            if (shouldRun) {
                try {
                    updateScreen();
                    Thread.sleep(1000);
                } catch (Throwable t) {
                    t.printStackTrace();
                    return;
                }
            }
            else {
                try {
                    Thread.sleep(100);
                }
                catch (Throwable t) {
                    t.printStackTrace();
                    return; 
                }
            }
        }
    }
    
    private void updateScreen() {
        // Snipped: update all the labels
        main.ctx.getRootContainer().repaint(180);
    }
    
}
Which is called from create():

Code:
    public void create(KindletContext context) {
        // Snipped: label creation and positioning

        thread = new WorkerThread(this);
        thread.start();
    }
(thread is a variable of the main Kindlet class)


This works when the kindle is freshly started. However, I noticed that it doesn't exit cleanly. I do have a destroy method that looks like this:

Spoiler:
Code:
    public void destroy() {
        
        thread.shouldExit = true;
        thread.interrupt();
    }


But it doesn't seem to be working properly. If I abort the restart when the kindle notices the failure, and relaunch the app, it no longer updates. That seems odd, since it should be creating a new instance. I don't really know how kindlet session/lifecycle management works, so I just shrugged and ignored it. As long as I only run the app once per session, it works fine.

Anyway, thanks again for the help with this. I don't know if I'll continue making anything for the kindle, as I prefer development environments that aren't actively working against me, but it was an interesting experience nonetheless.

ChronosDragon is offline   Reply With Quote