Timer in application

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Timer in application

marianciu
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

RE: Timer in application

Roger Whitcomb-2
So, it looks like your code is doing exactly what you're telling it to
do, namely sleep for 5 seconds and then update the display at the end.
You don't have any loop anywhere.  So, instead of using a Task,
considering doing something like this:

ApplicationContext.scheduleRecurringCallback(new Runnable() {
        @Override
        public void run() {
                // Update price with new value
                main.setValueBalancedPrice(/* new price */"103");
        }
}), 5000); // The 5000 value means to schedule the callback to
happen every 5 seconds

This has the additional advantage (besides being simpler) of also
running the callback function on the AWT event thread directly, so you
can update the display component directly inside the "run" method.

HTH,
~Roger Whitcomb

-----Original Message-----
From: marianciu [mailto:[hidden email]]
Sent: Friday, June 28, 2013 4:42 AM
To: [hidden email]
Subject: Timer in application

Hello

I have created application that will be changed prices after each 5
seconds (loop), and next prices will be updated on view.

I'm using this solution
http://pivot.apache.org/tutorials/background-tasks.html for run task
which generating new prices. I have problem, because only one times are
updating prices - when this task is ended. I would like, for prices will
be update after each 5 seconds.

Could you explain my how implementing it?

My code:
public class MainViewTest implements Application {

public class MainViewTest implements Application {

        public static void main(String[] args) {

                DesktopApplicationContext.main(MainViewTest.class,
args);
        }

        @Override
        public void startup(Display display, Map<String, String>
properties)
                        throws Exception {

                final MainWindow main = new MainWindow();
                main.open(display);

                main.setListnerForPushButtonBuy(new
ButtonPressListener() {

                        @Override
                        public void buttonPressed(Button button) {
                                UpdatePrice longCalculationTest = new
UpdatePrice();
                                TaskListener<String> taskListener = new
TaskListener<String>() {

                                        @Override
                                        public void
taskExecuted(Task<String> task) {
       
main.setValueBalancedPrice(task.getResult());
                                        }

                                        @Override
                                        public void
executeFailed(Task<String> task) {
       
System.err.println(task.getFault());
                                        }
                                };

                                longCalculationTest.execute(new
TaskAdapter<>(taskListener));
                        }
                });

        }

        @Override
        public boolean shutdown(boolean optional) throws Exception {

                return false;
        }

        @Override
        public void suspend() throws Exception {

        }

        @Override
        public void resume() throws Exception {

        }
}

public class UpdatePrice extends Task<String> {

        public UpdatePrice() {

        }

        @Override
        public String execute() throws TaskExecutionException {

                try {

                        Thread.sleep(5000);

                } catch (InterruptedException ex) {
                        throw new TaskExecutionException(ex);
                }

                return "103";
        }
}




--
View this message in context:
http://apache-pivot-users.399431.n3.nabble.com/Timer-in-application-tp40
22622.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

RE: Timer in application

marianciu
CONTENTS DELETED
The author has deleted this message.