简体   繁体   English

使用Java Swing进行线程化

[英]Threading with Java Swing

I have a swing worker which configures a serial port connection. 我有一个swing worker,它配置串行端口连接。 This process takes an indeterminable time to complete, sometimes 1 minute sometimes a lot more. 此过程需要花费不确定的时间才能完成,有时需要1分钟,有时甚至更多。

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. 否则,如果工作线程仍处于活动状态,则我希望执行等到工作线程完成为止。

You could check in your buttons ActionEvent if task.isDone() - where task is your SwingWorker - and continue only if it is true. 如果task.isDone() (其中task是您的SwingWorker task.isDone() ,则可以签入按钮ActionEvent ,并且只有在它为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. 然后,用户只有在按钮准备就绪后才能单击它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM