java swt进度条,从线程更新进度条时,SWT窗口无响应

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.

display.asyncExec(new Runnable() {

@Override

public void run() {

if (bar.isDisposed ())

return;

bar.setSelection(getProgress);

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值