【发布时间】:2012-12-20 19:07:43
【问题描述】:
我有一个可用的 ProgressMonitorDialog,但我想确保我设置正确。
首先是代码:
Dialog的创建方法
public void startProgressBar() {
try {
new ProgressMonitorDialog(getShell()).run(true, true,
new ProgressBarThread());
}
catch (InvocationTargetException e) {
MessageDialog.openError(getShell(), "Error", e.getMessage());
}
catch (InterruptedException e) {
MessageDialog.openInformation(getShell(), "Cancelled", e.getMessage());
}
}
类文件
class ProgressBarThread implements IRunnableWithProgress {
private static final int TOTAL_TIME = 1000;
public ProgressBarThread() {
}
public void run(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException {
monitor.beginTask("Creating PDF File(s): Please wait.....", IProgressMonitor.UNKNOWN);
for (int total = 0; total < TOTAL_TIME ; total++) {
Thread.sleep(total);
monitor.worked(total);
if (total == TOTAL_TIME / 2) monitor.subTask("Please be patient... Operation should finish soon.");
}
monitor.done();
}
}
调用 ProgressBar 并运行 Pdf 文件创建操作的方法
private void startSavePdfOperation() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
startProgressBar();
}
});
saveOp = new AplotSaveOperation(appReg.getString("aplot.message.SAVETOPDF"), "PDF", session);
saveOp.addOperationListener(new MyOperationListener(this) {
public void endOperationImpl() {
java.io.File zipFile = null;
try {
AplotSaveResultsParser.SaveResult saveResults = saveOp.getSaveResults();
if (saveResults != null) {
ETC..... ETC......
问题:
由于 ProgressMonitorDialog 是一个 GUI,它需要在一个 Display.getDefault().asyncExec?
如果 ProgressMonitorDialog 在单独的线程中运行,它如何知道在操作完成时关闭?
进度条和操作有关系吗?
我假设 ProgressBarThread 类中的 for 循环基本上是保持监视器打开的计时器是正确的?
有没有办法提高ProgressMonitorDialog的指标的速度,也可以去掉取消按钮吗?
这就是我认为目前正在发生的事情。
我正在启动 PDF 操作侦听器之前的进度条 参见上面的 startSavePdfOperation()
进度条以未知方式运行,但使用 for 循环保持进度条对话框打开,同时该操作在后台线程上运行。
参见上面的类 ProgressBarThread-
当 PDF 操作完成时,侦听器操作类关闭基本 GUI 对话框。
public void endOperation() { try { endOperationImpl(); } finally { Display.getDefault().asyncExec(new Runnable() { public void run() { w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_ARROW)); w.recursiveSetEnabled(getShell(), true); w.getShell().setEnabled(!getShell().getEnabled()); w.close(); } }); }}
我不确定 ProgressBarThread 监视器发生了什么?
这可能吗?
当 PDF 操作开始时,ProgressMonitorDialog 打开并启动指示器。可以保持未知。
PDF 操作完成后,监视器关闭,然后是基本对话框
我只想打开进度条对话框,通知用户他们的请求正在后台运行。
如上所述,上面的代码可以工作,但我担心关闭 Base GUI 会破坏我的 Progress Thread 和 Monitor,这不是一个好习惯。
【问题讨论】:
标签: swt progressmonitor