我做一个本地搜索的软件,点击开始按钮后后就把搜索的过程显示在text上,我想进行暂停操作,但是
这个时候界面内的东西都是不可点击的
(我用了asyncExec和BusyIndicator.showWhile(sShell.getDisplay(), paramer); 都不行),请问如何解决。

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【bestseal】截止到2008-07-06 15:51:06的历史汇总数据(不包括此帖):
    发帖的总数量:13                       发帖的总分数:250                      
    结贴的总数量:9                        结贴的总分数:180                      
    无满意结贴数:4                        无满意结贴分:110                      
    未结的帖子数:4                        未结的总分数:70                       
    结贴的百分比:69.23 %               结分的百分比:72.00 %                  
    无满意结贴率:44.44 %               无满意结分率:61.11 %                  
    楼主加油
      

  2.   

    /*
     * Created on July 7 2008
     * 
     * Copyright by 布谷鸟
     */
    package gui;import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    /**
     * 
     * @author cuckoo
     *
     */
    public class MultithreadingExercise { public static void main(String args[]){
    new MultithreadingExercise().createShell();
    }
    public void createShell(){
    _shell = new Shell(_display,SWT.MAX |SWT.MIN |SWT.RESIZE);
    _shell.setSize(500,400);
    _shell.setText("MultiThreading");
    createContents();
    _shell.open();
    while( !_shell.isDisposed()){
    if( !_display.readAndDispatch()){
    _display.sleep();
    }
    }
    _display.dispose();
    }
    public void createContents(){
    _displayLabel = new Label(_shell,SWT.NONE);
    _displayLabel.setText("Roll display");
    _displayLabel.setBounds(10, 10, 180, 15);
    _displayLabel.setBackground(new Color(_display,0,0,0));
    _displayLabel.setForeground(new Color(_display,140,140,142));

    _stopButton = new Button(_shell,SWT.PUSH);
    _stopButton.setBounds(200, 10, 40, 25);
    _stopButton.setText("Stop");
    _stopButton.addSelectionListener(new SelectionAdapter(){
    public void widgetSelected(SelectionEvent e) {
    // TODO Auto-generated method stub
    _stopThread = true ;
    }
    });
    ProcessThread();
    }
    /** create a thread */
    public void ProcessThread(){
    Thread thread = new Thread(){
    public void run() {
    // TODO Auto-generated method stub
    while( !_stopThread){
    _index++;
    _display.asyncExec(new Runnable(){
    public void run() {
    // TODO Auto-generated method stub
    _displayLabel.setText("布谷鸟 //-->"+_index);
    }
    });
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    };
    thread.start();
    }
    private Label _displayLabel = null;
    private Button _stopButton = null;
    private Shell _shell = null;
    private Display _display = Display.getDefault();
    private boolean _stopThread = false;
    private int _index = 0;









    }
      

  3.   

    这样写没问题啊,lz再确认一下吧 
    -------------------------------------------------------------
                Quietly through  .....
      

  4.   

    先谢谢布谷鸟的回答,我的问题可能你没有弄清楚,你写这个代码是程序启动的时候有一个线程,通过另一个线程结束主线程。
    现在我做的是通过开始按钮打开一个线程进行搜索,搜索内容实时的显示在界面上,代码如下

    Thread thread = new Thread() {
    public void run() {
    if (!display.isDisposed()) {
    Runnable runnable = new Runnable() {
    public void run() {
    textArea.setText("");
    MainControl.goOnFlag = true;
    MainControl.showSystemFile();
    }
    };
    display.syncExec(runnable);
    }
    }
    };
    thread.start();
    当我启动这个线程以后界面是不可操作的,就是其他按钮是不可点击的,我不知道是这个问题如何解决,谢谢。
      

  5.   

    我在想会是搜索的时候数据量太大了才不能操作界面么?CPU使用率大概是百分之50左右。
      

  6.   

    因为后台线程和前台线程是两个线程,这就涉及到要用后台线程访问前台线程。
    当后台线程要访问前台界面的时候一定要用到Display对象
    比如: 
    Display.getDefault().syncExec(new Runnable() {
    public void run() {
    //访问前台的代码
    }
    });希望能对你有所帮助~~
      

  7.   

    谢谢楼上,syncExec和asyncExec的方法我都用过,但是问题不在这里,是我要处理的数据比较大,所以界面上的数据一直在滚动,但是按钮没有反应,不能点击。
      

  8.   

     public void createContents(){
            _displayLabel = new Label(_shell,SWT.NONE);
            _displayLabel.setText("Roll display");
            _displayLabel.setBounds(10, 10, 180, 15);
            _displayLabel.setBackground(new Color(_display,0,0,0));
            _displayLabel.setForeground(new Color(_display,140,140,142));
            
            _stopButton = new Button(_shell,SWT.PUSH);
            _stopButton.setBounds(200, 10, 40, 25);
            _stopButton.setText("Stop");
            _stopButton.addSelectionListener(new SelectionAdapter(){
                public void widgetSelected(SelectionEvent e) {
                    // TODO Auto-generated method stub
                    _stopThread = true ;
                }
            });
            ProcessThread();
        }
      

  9.   


    private static void showSubFile(File file){
    if(file.isDirectory()){
    File [] subFile = null;
    if(goOnFlag){
    subFile = file.listFiles();
    if(subFile == null){
    return;
    }
    for(int i = 0; i< subFile.length; i++){
    if(!goOnFlag){
    return;
    }
    Text textArea = MainFrame.textArea;
    Text text = MainFrame.text;
    textArea.append(subFile[i].getAbsolutePath()+ Source.lineChangeTag);
    if(subFile[i].getName().equals(text.getText())){
    textArea.append("**************************************************" +
    Source.lineChangeTag+"找到了!!文件在:"+Source.lineChangeTag
    + subFile[i].getAbsolutePath()+ Source.lineChangeTag); goOnFlag = false;
    return;
    }
    if(subFile[i].isDirectory()){
    showSubFile(subFile[i]);//去掉或不去掉
    }
    }
    }
    else{
    return;
    }
    }我就是这么递归的,只要把那句注释掉就可以操作按钮,不注释掉就无法控制按钮,整个这部分的操作都是由界面那边另外的线程启动的。
      

  10.   

    有人用递归的时候线程出过问题么?我把这部分代码用Swing实现了一下就没有问题,是不是SWT的线程递归调用的时候会出问题?