Clarification on how Codename One UITimer works
I need a clarification about the Codename One UITimer
.
For example, if I want to execute the same code every two seconds, a code like UITimer.timer(2000, true, () -> { do something; });
works until the user stays in the current Form
? Is that right?
If I want to execute the same code every two seconds regardless the shown Form
, have I to use something different from UITimer
, like a custom separate thread? For example the following code?
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
EasyThread.start("MyTimer").run(() -> {
boolean executeMe = true;
while (executeMe) {
Log.p("Do something every two seconds...");
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Log.p("Stopping the EasyThread "MyTimer" because an exception");
Log.e(ex);
executeMe = false;
}
}
});
Moreover, if I execute this example code, the first logged line is:
[MyTimer] 0:0:0,59 - Codename One revisions: 8b451ecb7bfbe60baf91006441e7d7d9c46afe09
Why is that line logged by my custom thread instead of by the EDT?
codenameone
add a comment |
I need a clarification about the Codename One UITimer
.
For example, if I want to execute the same code every two seconds, a code like UITimer.timer(2000, true, () -> { do something; });
works until the user stays in the current Form
? Is that right?
If I want to execute the same code every two seconds regardless the shown Form
, have I to use something different from UITimer
, like a custom separate thread? For example the following code?
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
EasyThread.start("MyTimer").run(() -> {
boolean executeMe = true;
while (executeMe) {
Log.p("Do something every two seconds...");
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Log.p("Stopping the EasyThread "MyTimer" because an exception");
Log.e(ex);
executeMe = false;
}
}
});
Moreover, if I execute this example code, the first logged line is:
[MyTimer] 0:0:0,59 - Codename One revisions: 8b451ecb7bfbe60baf91006441e7d7d9c46afe09
Why is that line logged by my custom thread instead of by the EDT?
codenameone
add a comment |
I need a clarification about the Codename One UITimer
.
For example, if I want to execute the same code every two seconds, a code like UITimer.timer(2000, true, () -> { do something; });
works until the user stays in the current Form
? Is that right?
If I want to execute the same code every two seconds regardless the shown Form
, have I to use something different from UITimer
, like a custom separate thread? For example the following code?
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
EasyThread.start("MyTimer").run(() -> {
boolean executeMe = true;
while (executeMe) {
Log.p("Do something every two seconds...");
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Log.p("Stopping the EasyThread "MyTimer" because an exception");
Log.e(ex);
executeMe = false;
}
}
});
Moreover, if I execute this example code, the first logged line is:
[MyTimer] 0:0:0,59 - Codename One revisions: 8b451ecb7bfbe60baf91006441e7d7d9c46afe09
Why is that line logged by my custom thread instead of by the EDT?
codenameone
I need a clarification about the Codename One UITimer
.
For example, if I want to execute the same code every two seconds, a code like UITimer.timer(2000, true, () -> { do something; });
works until the user stays in the current Form
? Is that right?
If I want to execute the same code every two seconds regardless the shown Form
, have I to use something different from UITimer
, like a custom separate thread? For example the following code?
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
EasyThread.start("MyTimer").run(() -> {
boolean executeMe = true;
while (executeMe) {
Log.p("Do something every two seconds...");
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Log.p("Stopping the EasyThread "MyTimer" because an exception");
Log.e(ex);
executeMe = false;
}
}
});
Moreover, if I execute this example code, the first logged line is:
[MyTimer] 0:0:0,59 - Codename One revisions: 8b451ecb7bfbe60baf91006441e7d7d9c46afe09
Why is that line logged by my custom thread instead of by the EDT?
codenameone
codenameone
asked Jan 1 at 10:36


Francesco GalganiFrancesco Galgani
1,4191613
1,4191613
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Yes, UITimer
is an animation associated with the current Form
. It doesn't draw anything but uses the builtin animation mechanism which runs on the EDT. Notice that if you leave a form and return to it the timer will continue e.g.:
- Opened form at 0 time and set a timer for 15 seconds
- Went to different form at 7 seconds
- Returned to original after 30 seconds - the timer will fire immediately on return
You can also use a regular Timer
or Thread
. However, for your specific code EasyThread
doesn't provide a benefit over a regular thread since it runs in an infinite loop. The benefit of EasyThread
is in it's job processing ability.
Notice that you would need to use callSerially
to return to the EDT when working with such timers/threads.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994773%2fclarification-on-how-codename-one-uitimer-works%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, UITimer
is an animation associated with the current Form
. It doesn't draw anything but uses the builtin animation mechanism which runs on the EDT. Notice that if you leave a form and return to it the timer will continue e.g.:
- Opened form at 0 time and set a timer for 15 seconds
- Went to different form at 7 seconds
- Returned to original after 30 seconds - the timer will fire immediately on return
You can also use a regular Timer
or Thread
. However, for your specific code EasyThread
doesn't provide a benefit over a regular thread since it runs in an infinite loop. The benefit of EasyThread
is in it's job processing ability.
Notice that you would need to use callSerially
to return to the EDT when working with such timers/threads.
add a comment |
Yes, UITimer
is an animation associated with the current Form
. It doesn't draw anything but uses the builtin animation mechanism which runs on the EDT. Notice that if you leave a form and return to it the timer will continue e.g.:
- Opened form at 0 time and set a timer for 15 seconds
- Went to different form at 7 seconds
- Returned to original after 30 seconds - the timer will fire immediately on return
You can also use a regular Timer
or Thread
. However, for your specific code EasyThread
doesn't provide a benefit over a regular thread since it runs in an infinite loop. The benefit of EasyThread
is in it's job processing ability.
Notice that you would need to use callSerially
to return to the EDT when working with such timers/threads.
add a comment |
Yes, UITimer
is an animation associated with the current Form
. It doesn't draw anything but uses the builtin animation mechanism which runs on the EDT. Notice that if you leave a form and return to it the timer will continue e.g.:
- Opened form at 0 time and set a timer for 15 seconds
- Went to different form at 7 seconds
- Returned to original after 30 seconds - the timer will fire immediately on return
You can also use a regular Timer
or Thread
. However, for your specific code EasyThread
doesn't provide a benefit over a regular thread since it runs in an infinite loop. The benefit of EasyThread
is in it's job processing ability.
Notice that you would need to use callSerially
to return to the EDT when working with such timers/threads.
Yes, UITimer
is an animation associated with the current Form
. It doesn't draw anything but uses the builtin animation mechanism which runs on the EDT. Notice that if you leave a form and return to it the timer will continue e.g.:
- Opened form at 0 time and set a timer for 15 seconds
- Went to different form at 7 seconds
- Returned to original after 30 seconds - the timer will fire immediately on return
You can also use a regular Timer
or Thread
. However, for your specific code EasyThread
doesn't provide a benefit over a regular thread since it runs in an infinite loop. The benefit of EasyThread
is in it's job processing ability.
Notice that you would need to use callSerially
to return to the EDT when working with such timers/threads.
answered Jan 1 at 13:00


Shai AlmogShai Almog
40.9k52555
40.9k52555
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994773%2fclarification-on-how-codename-one-uitimer-works%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown