开发者

Blackberry service thread hangs the ui when executing

In my blackberry app i am using alternate entry point and run a thread on startup which does http operation and the same thread is called repeatedly after some time lets say 3 minutes. It does its operation but the problem is it hangs the ui here is my code.

final class sendUnsentService extends Thread {

Timer timer;
TimerTask repeatMe;

sendUnsentService me;

boolean working = false;

public boolean isWorking() {
    return working;
}

public void interrupt() {
    super.interrupt();

    System.out.println("___________________________[STOPSERVICE()]");
    try {
        timer.cancel();
    } catch (Exception e) {
        System.out
        .println("_______________________[PROBLEM STOPPING SERVICE]");
    }
}

public void run() {
    super.run();
    System.out.println("___________________________[STARTSERVICE()]");
    new Thread() {
        public void run() {
            timer.schedule(repeatMe, 0, 300000);
        };
    }.start();
}

public sendUnsentService() {
    me = this;
    Imag开发者_JS百科eUtils.initPersistentStorage();
    timer = new Timer();

    repeatMe = new TimerTask() {

        public void run() {
            working = true;
            if (sendUnsentActivity()) {
                me.interrupt();
            }
            working = false;
        }
    };
}

I am invoking this thread on some user interaction using this code and then it hangs the ui

try {
    helloBerry.service = new sendUnsentService();
} catch (Exception e) {
System.out.println("_____________1 " + e);
}
try {
    helloBerry.service.startService();
} catch (Exception e) {
System.out.println("_____________2 " + e);
}

and this is how i call the service to start on device booting in main method using alternate entry point

if (args != null && args.length > 0 && args[0].equals("normal")) {
            System.out.println("_________[STARTING APP]");
___________some code here to show a screen
            }
        } else {
            System.out.println("_________[STARTING SERVICE]");
            service = new sendUnsentService();
            service.run();
        }


First thing I notice is that you're starting a TimerTask from another Thread. A Timer contains its own Thread, so this is unnecessary -- just call it directly. Also, I don't think you need to make this extend a Thread. What you might want to do is use the TimerTask to reschedule itself if it doesn't error, otherwise just let it stop.

As far as locking up the UI, unless you're doing some odd synchronizing on a RuntimeStore, this shouldn't affect your main program. It should be running as two separate processes, unaware of each other. What circumstances does it freeze under?


OMG! Finally i found the thing and its kind of stupid the problem was some sysout statements which i was using to print the data which i was sending to server thank you jprofit for taking interest

i have got another question related to this if you can answer i am starting this thread on some event so when user closes the application it also close this thread i have tried extending application instead of thread but i an not able to handle it remotely.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜