Threading issue, fields doesn't update

Asycntask's onprogressupdate is always being called, :

    @Override
    protected void onProgressUpdate(Integer... values) {
        pbSubStatus.incrementProgressBy(values[1]);
        rawNum = values[0];
                     ...
    }

And on outer class:

    private double getCurrentTime() {
    return currentTime;
}
private void doSomethingOnThread() {
    LinearLayout layout = (LinearLayout) findViewById(R.id.main);
    sMeter = new Meter(context);
    layout.addView(sMeter);
    final Handler mHandler = new Handler();
    new Thread() {
        @Override
        public void run() {

            while (running) {
                previousTime = getCurrentTime();
                previousNum = getRawNum();
                try {
                    Thread.sleep(1500);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                currentTime=getCurrentTime();
                currentNum = getRawNum();
                mHandler.post(mUpdateResults);
            }
        }
    }.start();
}
final Runnable mUpdateResults = new Runnable() {
    public void run() {
        updateResultsInUi();
    }
};
private void updateResultsInUi() {
    if(previousNum>currentNum){
        return;//TODO
    }else{
        int temp=currentNum-previousNum;//Problem here, both num are same.
        double timeTemp=currentTime-previousTime; 
        someField=  (float)( temp/timeTemp);
    }
    sMeter.setTarget(someField);//this should be last line to be executed in this snippt
}

So the problem is previousNum and currentNum are always returning the same number. I want the thread to wait 1.5 sec to get a new (getRawNum()) from the onProgressupdate. But I don't understand they they are returning the same number. Thanks for any advice.

Answers


I don't think this section is doing what you want:

while (running) {
    previousTime = getCurrentTime();
    previousNum = getRawNum();
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    currentTime=getCurrentTime();
    currentNum = getRawNum();
    mHandler.post(mUpdateResults);
}

For instance, let's say this starts executing at time=0. At the end of the first iteration you will have:

previousTime = 0
currentTime  = 1500

...however, the loop will immediately start its second iteration, and then you will have:

previousTime = 1500
currentTime  = 1500

...then it will sleep for another 1500 milliseconds and set currentTime to 3000, and then immediately start the next iteration, setting previousTime to 3000 as well.

So the problem is that for the vast majority of the time, your code is leaving currentTime and previousTime set to the same value. The only time they have different values is for the extremely small span of time in between when one iteration ends and the next iteration begins.

A quick way to work around this problem is as follows:

currentTime = getCurrentTime();
currentNum = getRawNum();
while (running) {
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    previousTime = curentTime;
    previousNum = currentNum;
    currentTime=getCurrentTime();
    currentNum = getRawNum();
    mHandler.post(mUpdateResults);
}

A better approach might be to refactor your code to use something like a circular buffer to keep track of the last n samples and sample timestamps. Then you can simply query for a new sample at regular intervals, and when updating the UI you just compare the most recent thing in the buffer against the one that came immediately before it.

And as for fields in your UI not updating, make sure that the code you have that tries to update them is running on the main thread.


Need Your Help

C Programming: Scanf in while loop only reads input once and then terminates

c while-loop char character scanf

I am new in C programming and I am currently learning about while loops. The problem I have is, that the while loop must continue until the user wishes to terminate the while loop. But when i run m...

Orbeon 4.5 - How to pass request parameters?

orbeon

We are upgrading from Orbeon 3.9 to 4.5.