I am writing an java swt application to process the server log files and generate a excel sheet with some analytics and I have no issues doing that.
The problem arises when I try to update the progress bar.
Here is a basic flow of the code. The user selects the log file using a directoryDialog and presses a start button. When the button is clicked the following code is executed.
startButton.addMouseListener(new MouseAdapter() {
public void mouseUp(MouseEvent e) {
pm.setUserData(ud,progressBar);
progressBar.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
pm.startProcessing();
}
});
}
In the class where the actually processing of the file happens there is simple while loops that loops till it reaches the end of the file. After each line is read from the file I call a method which updates the progress bar.
while ((logEntry = br.readLine()) != null) {
readSize += logEntry.length() + 1;
//some long processing logic
progressBar.setSelection(getProgress());
}
getProgress() is a simple method which calculates percentile of the file read
(read/total)*100
The above program is working. I am getting the expected result. The progress bar updates but the gui becomes unresponsive. I cannot move the window. I cannot click the close button.
I tried using swing worker and display.asyncExec()
The SWT documentation clearly states that ProgressBar.setSelection()
will throw an exception (ERROR_THREAD_INVALID_ACCESS
) when not called from the UI thread. That means you either swallow exceptions somewhere in your code or the code above runs in the UI thread.
There is a snippet which shows how to update a ProgressBar
from a thread:
display.asyncExec(new Runnable() {
@Override
public void run() {
if (bar.isDisposed ())
return;
bar.setSelection(getProgress);
}
});