I have a swing worker which configures a serial port connection. This process takes an indeterminable time to complete, sometimes 1 minute sometimes a lot more.
My problem arises when users click a button that needs configuration data whilst the worker thread is still configuring.
I would like to know how to execute a user's request only if the worker thread has completed. Else if worker thread in still alive, I want execution to wait until worker thread has finished.
add PropertyChangeListener
to SwingWorker
, then you can to determine of status from SwingWorker
have to implemented get()
in the SwingWorker
's method done()
, otherwise isn't possible to get an exeption(s) from invoked methods
depend of code but I doubt that is possible to change setting, untill current thread ended, better could be to SwingWorker#cancel()
and then to restart this instance
You could check in your buttons ActionEvent
if task.isDone()
- where task is your SwingWorker
- and continue only if it is true. But you might want to show a popup or something, otherwise the user might get confused why nothing is happening.
Another simple solution is to expose the button and disable it while the task is running and enable it again when it's finished. Then the user can't click the button until it's ready.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.