ybc3066978 / roottools

Automatically exported from code.google.com/p/roottools
0 stars 0 forks source link

Block Until Command has been processed #50

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,

since the latest version the waitFor has been removed. Now i try to implement 
it on my own and let the script run until it has been done.

This is how i do it (currently) but it doesnt looks like this is the way how it 
should be.

Command command = new Command(0, "chmod 777 /sdcard/test") {
        @Override
        public void commandOutput(int id, String line) {
        }

        @Override
        public void commandTerminated(int id, String reason) {
        }

        @Override
        public void commandCompleted(int id, int exitCode) {
            Log.d(TAG, "Command completed");
        }
    };

    RootTools.getShell(true);
    Shell.runRootCommand(command);
    while (!command.isFinished()) {
        Thread.sleep(1);
    }
    Log.d(TAG, "now the command is finished");

Im using the rootools in intentservices/asynctask. Blocking is not a problem 
but its important in my case. Any idea how to use it?

Original issue reported on code.google.com by emanuel....@gmail.com on 25 Sep 2013 at 11:04

GoogleCodeExporter commented 9 years ago
This should work. However, I have a couple of suggestions...

Don't use Thread.sleep, instead use Thread.wait().

If you are going to pass in an amount to wait, wait a bit longer before 
checking to make it more efficient.

Also, you don't need to pass in a time to wait since you can wait on command to 
let you know when it has finished. You can do the following:

command.wait();

A notifyAll is called when the command is finished and your lock will be 
released.

Original comment by Stericso...@gmail.com on 7 Oct 2013 at 1:52