SWT界面上有个开始按钮和结束按钮,我想实现点击这个开始按钮后,去定期执行一个很费时的查询任务,
查询的返回结果要增加到一个树节点上,现在主要的问题就是,
我点击开始按钮后,在执行查询任务的过程中,界面没响应了,也不能点结束按钮,必须要等到查询任务完成后界面才能响应
下面是开始按钮监听代码,那个timer是成员变量,period是一个Long型的时长
在网上查了几个小时了,试了好多方法也没解决,有这方面经验的麻烦指点下,谢谢。
private Timer timer; ...... startButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { statusLabel.setText("正在获取信息..."); if (timer != null) { timer.cancel(); } timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { display.asyncExec(new Runnable() { public void run() { discovery(); } }); } }, 0, period); } }); ...... public void discovery() { final SnmpDiscovery discovery = new SnmpDiscovery(); resultMap = discovery.discovery(resultMap); if (null != resultMap) { for (Iterator it = resultMap.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); // 这里有访问到SWT UI线程中的变量 TreeItem item = new TreeItem(deviceTree, SWT.NONE); item.setText(String.valueOf(entry.getKey())); } } }