说明代号一个UITimer的工作方式 [英] Clarification on how Codename One UITimer works
问题描述
我需要对代号One UITimer
进行澄清。
I need a clarification about the Codename One UITimer
.
例如,如果我想执行每两秒钟使用相同的代码,例如 UITimer.timer(2000,true,()-> {做某事;});
这样的代码会一直工作到用户停留在当前位置表格
?是吗?
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?
如果我想每两秒钟执行一次相同的代码,不管显示的表格
,我是否可以使用与 UITimer
不同的东西,例如自定义单独的线程?例如以下代码?
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;
}
}
});
此外,如果我执行此示例代码,则记录的第一行是:
[MyTimer] 0:0:0,59-代号一个修订:8b451ecb7bfbe60baf91006441e7d7d9c46afe09
为什么该行由我的自定义线程而不是EDT记录?
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?
推荐答案
是的, UITimer
是与当前关联的动画表格
。它没有画任何东西,只是使用了在EDT上运行的内置动画机制。请注意,如果您离开表格并返回表格,计时器将继续运行,例如:
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.:
- 在0时间打开表格并将计时器设置为15秒
- 在7秒时采用其他形式
- 30秒后恢复为原始状态-计时器将在返回时立即触发
您还可以使用常规的 Timer
或 Thread
。但是,对于您的特定代码, EasyThread
不能提供比常规线程更多的好处,因为它在无限循环中运行。 EasyThread
的好处在于它的工作处理能力。
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.
请注意,您需要使用 callSerially
在使用此类计时器/线程时返回到EDT。
Notice that you would need to use callSerially
to return to the EDT when working with such timers/threads.
这篇关于说明代号一个UITimer的工作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!