简体   繁体   English

JavaFX Node.snapshot()从工作线程调用时冻结线程,想法?

[英]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.

相关问题 JavaFX应用程序线程减慢然后冻结 - JavaFX Application Thread slows and then freezes android应用程序冻结在工作线程中运行的代码 - android app freezes from code running in worker thread 当我运行另一个线程时,JavaFx进度指示器冻结 - JavaFx Progress Indicator freezes when I run another thread 必须从UI线程调用方法,当前推断的线程为worker - Method must be called from the UI thread, currently inferred thread is worker 当工作线程在android中完成其工作时如何从工作线程向主线程获取通知 - How to get notification from worker thread to main thread when worker thread done with its work in android 在Android中在辅助线程上编写时从主线程获取SQLite? - Fetch SQLite from main thread when writing on worker thread in Android? 方法publishProgress必须从工作线程调用,当前推断的线程是主线程 - Method publishProgress must be called from the worker thread, currently inferred thread is main thread Node.snapshot(null, null) 改变场景的大小 - Node.snapshot(null, null) changes size of Scene SwingWorker:取消工作线程后调用done()时引发的错误 - SwingWorker: error thrown when done() is called after worker thread is cancelled 必须从UI线程调用方法getText,当前推断的线程为worker - Method getText must be called from the UI thread, currently inferred thread is worker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM