- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的 java 程序中使用 Processbuilder 在后端执行一个外部进程。我正在使用 process.waitFor 来获取进程的退出状态。后端进程需要时间,具体取决于所提供的输入。所以我需要为不确定的时间制作一个进度条,直到进程完成。这只是为了通知用户后端正在进行一个进程。
try {
button.setEnabled(false);
this.progressbar.setVisible(true);
this.progressbar.setIndeterminate(true);
}
catch(Exception e)
{
e.printStackTrace();
}
perform(this);
}
}
public void perform(JFrame f)
{
String cmd[]={"cmd", "/c","a.exe:};
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.directory(new File(dir_path));
pb.redirectErrorStream(true);
try {
p = pb.start();
} catch (IOException ex) {
Logger.getLogger(execution.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
int exitVal = p.waitFor();
if(exitVal>0)
{
this.progressbar.setVisible(false);
this.progressbar.setIndeterminate(false);
this.button.setEnabled(true);
}
if(p.waitFor()==0)
{
this.progressbar.setVisible(false);
this.progressbar.setIndeterminate(false);
JOptionPane.showMessageDialog(f,"Scan completed successfully.");
}
} catch (InterruptedException ex) {
Logger.getLogger(execution.class.getName()).log(Level.SEVERE, null, ex);
}
}
Button 是调用调用的我的按钮的名称。但我面临的问题是函数调用 perform 是在没有执行这些语句的情况下进行的。
try {
button.setEnabled(false);
this.progressbar.setVisible(true);
this.progressbar.setIndeterminate(true);
}
catch(Exception e)
{
e.printStackTrace();
}
按钮在该函数执行后被禁用。1)我可以知道为什么会这样吗?2) 我可以知道使进度条指示后端进度直到完成为止的解决方案吗?
提前致谢。
最佳答案
我建议使用 SwingWorker
。它是为解决诸如此类的问题而设计的。这是我的代码库中的一些代码片段(vui
是一个 JFrame):
vui.clearOutput();
vui.setOutput("Waiting for items to copy...\n"
+ "This could take several minutes. Please standby...");
vui.disableExit();
vui.disableInput();
vui.disableNext();
vui.showProgressBar();
// make thread so you can disable all options when zipping and enable progress bar
SwingWorker transferWorker = new SwingWorker() {
@Override
protected Void doInBackground() throws Exception {
try {
Process p = Runtime.getRuntime().exec("resources/bin/transferCopier.bat");
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
p.waitFor();
} catch (IOException ex) {
Logger.getLogger(VaderController.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ie) {
Logger.getLogger(VaderController.class.getName()).log(Level.SEVERE, null, ie);
}
return null;
}
@Override
public void done() {
vui.hideProgressBar();
vui.clearOutput();
vui.setOutput("Transfer Complete!\n\n"
+ "Push \"Exit\" to quit.\n\n");
vui.enableExit();
mode = VaderMode.END;
}
};
transferWorker.execute();
当执行 transferWorker.execute();
时,调用 doInBackground()
。一旦 doInBackground()
完成计算,就会调用 done()
。 done()
是您对 GUI 进行任何最终更新的地方。
我上面的代码需要注意的关键是,我在执行 SwingWorker
之前启用了进度条,然后当 SwingWorker
执行完毕后,我禁用了进度条.
资源:
http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html
关于java - 进度监视器(不确定)以显示执行发生在后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15901648/
以下是否意味着只有一个线程可以在对象的任何方法中?或者多个线程可以使用不同的方法而不是同一个方法吗?为什么? public class SynchronizedCounter { privat
我有一个 java 监视器,但我需要一些解释: class Test { private int data; private boolean full = false; public sy
#include #include #include #include #define N_ASS 4 pthread_t tid[N_ASS]; //mutex pthread_mutex_
我想知道是否有一种工具可以跟踪我的 C# 程序正在使用/访问的文件列表(文本/任何外部文件)? =D 有什么工具吗? 附注它是为了测试程序安全性.. ;) 最佳答案 ProcessMonitor监控任
我想知道 Monitor 类。据我所知,所有等待线程都不是 FIFO。第一个获得锁的并不总是等待队列中的第一个。这样对吗?有什么方法可以确保 FIFO 条件? 问候 最佳答案 如果您指的是内置方式,则
我有一个 ASP.net (c#) 应用程序,其中包含修改全局可访问资源(如 web.config 文件)的部分代码。当然,在修改资源时,为了防止竞争条件,一次只允许一个用户,因此我需要使用监视器锁定
如何方便调试持久化上下文的状态、观察查询结果、监控所有实体? 有一些 JPA 监视器可以用于此吗? 最佳答案 如果您使用 EclipseLink,则有一个性能监视器选项, 看, http://wiki
启动和停止按钮在监视器 tomcat 中被禁用,大约 10-15 分钟后,它允许我重新启动服务器 当 tomcat 停止响应并且我尝试重新启动服务器时,我遇到了这个问题,我能够停止服务器,但之后它不允
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我必须开发一个系统来监控报告的生成/传输。 系统数据将存储在数据库表中(Sybase) 报告将按不同的时间表生成(“周一至周五晚上 10 点”、“周六早上 5 点”、“每月的第一天”等) 系统只会监控
我需要同步多个线程(使用 POSIX 线程)。此外,我正在使用条件变量(监视器)来实现这一点。 问题是我必须实现“先到先得”的策略。假设多个线程正在等待另一个线程发出条件变化的信号,pthread_c
在 eclipse 中,我有一个运行的 weblogic 服务器,并部署了一个 j2ee 应用程序。该应用程序在端口 7001 上提供服务。我想将监视器连接到应用程序,我不知道要使用什么端口。我想我知
我正在使用芹菜和花卉。当我访问Flower中的“任务”标签时,我可以看到我的任务正在注册,甚至可以在“状态”列中看到“成功”标签以及所有内容。 但是,在“监视器”选项卡上,所有图形(“成功任务”,“失
有人知道有一个可以监控 beanstalkd 队列的应用程序吗?我正在寻找一些可以显示管道和工作统计信息并允许您检查详细信息的东西。 我对语言/平台并不是很挑剔,只是想在编写自己的语言/平台之前知道是
使用 Microsoft.Azure.Management.Monitor 的预览包,我尝试将指标从 Azure 获取到 .NET Core 应用程序中,但我不确定要输入什么内容作为“resource
使用 Microsoft.Azure.Management.Monitor 的预览包,我尝试将指标从 Azure 获取到 .NET Core 应用程序中,但我不确定要输入什么内容作为“resource
我想知道是否有一种方法可以通过客户端应用程序连接到位于 WebLogic 服务器上的业务 Activity 监视器。我想用 BAM 语句替换 JMS 生产者/消费者客户端中的日志语句,以便使用消息进度
我的网站有一个DIV,它的高度不固定。当用户在图像上移动鼠标时,将出现此 DIV 并显示有关图像的一些信息。页面上有几个网格格式的图像,每个图像都有自己的信息。很明显,一些图像位于屏幕底部,因此通过
提醒:Arch Linux 使用 pacman 而不是 apt-get 所以我有一个想法,我希望能够离开我的房间,并且仍然可以看到我手机的下载进度。我曾寻找过已有的程序,但一无所获,所以我决定自己编写
如果连接到多个显示器,如何使用 python 制作屏幕截图? 我试过: import sys from PyQt4.QtGui import QPixmap, QApplication app = Q
我是一名优秀的程序员,十分优秀!