我已爲Eclipse插件實施了一個嚮導,顯示了多個頁面。其中一個頁面需要一些冗長的初始化,這意味着它包含一個SWT表,它需要由來自外部源的信息填充。此源需要先激活(在幾秒鐘後返回一個單一的方法調用 - 我無法預先知道需要多久),然後才能將其用作表查看器的輸入。這個初始化當前是由表模型提供者在需要第一次訪問外部源時完成的。如何在Eclipse嚮導中正確更新未知持續時間操作的進度條?
因此,當我進入嚮導頁面時,我想顯示一個虛擬的進度條,只是計數一段時間。我的做法是下面的,但遺憾的是沒有在所有的工作:
private void initViewer() {
IRunnableWithProgress runnable = new IRunnableWithProgress() { // needed to embed long running operation into the wizard page
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
SubMonitor progress = SubMonitor.convert(monitor);
Thread thread = new Thread() {
@Override
public void run() {
Display.getDefault().syncExec(new Runnable() {
public void run() {
viewer.setInput(ResourcesPlugin.getWorkspace().getRoot()); // this will make the table provider initialize the external source.
}
});
}
};
thread.start();
while(thread.isAlive()) {
progress.setWorkRemaining(10000);
progress.worked(1);
}
progress.done();
}
};
try {
getContainer().run(false, false, runnable);
} catch(Exception e) {
throw new Exception("Could not access data store", e);
}
}
這種方法時,嚮導頁的調用setVisible()獲取然後調用 - 方法被調用,並應,幾秒鐘後,設定瀏覽器的輸入。然而,這從來就不會發生,因爲最內層的run()方法永遠不會被執行。
有關Eclipse嚮導中如何處理長時間運行(其中沒有準確估計值)的任何提示將非常感謝!
我沒有看到你的方法的價值。只要你不知道它會持續多久,給用戶的反饋就是錯誤的。爲什麼不測量一段時間內整個操作的長度,然後決定每步應該做多少滴答?當你用'beginTask(String,int)'開始時,int應該是整個跨度。請參閱[IProgressMonitor API](http://help.eclipse.org/helios/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/runtime/IProgressMonitor.html)。 – mliebelt
感謝您的評論。我的目標是讓用戶反饋一些事情正在發生,而不僅僅是阻止用戶界面。外部資源(實際上基於文件的三重存儲)的初始化只需要幾秒鐘(5-10),但不能真正被測量/估計。它發生在單個框架方法中,並且取決於已存儲的數據量。 – mwuersch