[英]JavaFX Node.snapshot() freezes thread when called from worker thread, ideas?
I need to write a javafx.scene.canvas.Canvas
to a javafx.scene.image.WritableImage
. 我需要将
javafx.scene.canvas.Canvas
写入javafx.scene.image.WritableImage
。 I am aware that the snapshot() method in javafx.scene.Node
will perform this, and it works great when I'm on the JavaFX Application thread. 我知道
javafx.scene.Node
中的snapshot()方法将执行此操作,当我位于JavaFX Application线程上时,它的效果很好。
But I need to perform this write from a worker thread that utilizes the javafx.concurrent
package. 但是我需要从使用
javafx.concurrent
包的工作线程执行此写操作。 So I have a javafx.concurrent.Task<V>
that attempts to call the snapshot()
method from within its call()
method. 因此,我有一个
javafx.concurrent.Task<V>
,尝试从其call()
方法内call()
snapshot()
call()
方法。 When attempting to do this, the thread freezes when it calls snapshot()
. 尝试执行此操作时,线程在调用
snapshot()
时冻结。
So my question is am I allowed to call snapshot()
from a worker thread from that utilizes the javafx.concurrent
package? 所以我的问题是我可以从使用
javafx.concurrent
包的工作线程中调用snapshot()
吗?
Try Platfrom#runLater()
which was introduced exactly for this kind of tasks: http://docs.oracle.com/javafx/2/api/javafx/application/Platform.html#runLater(java.lang.Runnable) 尝试
Platfrom#runLater()
针对此类任务介绍的Platfrom#runLater()
: http : Platfrom#runLater()
)
Platform.runLater(new Runnable() {
public void run() {
myNode.snapshot(...)
}
}
You can run this code from the any thread, but snapshot()
method would be called on FX App Thread as required. 您可以从任何线程运行此代码,但是会根据需要在FX App Thread上调用
snapshot()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.