在SWT开发中,我开发一个程序,里面有2个按钮,一个搜索,
一个是停止,搜索按钮事件中使用了一个while循环进行搜索。
我想实现的效果是:点击停止按钮的就终止while循环,然后把
搜索结果显示在另外一个窗口。实际上我已经实现搜索和显示的
功能,但是在点击停止的时候就会出现程序无响应,即程序死掉
的情况,也就是会所我无法实现停止这个功能。protected void createContents() {
shell = new Shell();
shell.setSize(500, 375);
shell.setText("程序测试"); final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {]
while(...){
...
}
}
});
button.setText("搜索");
button.setBounds(156, 144, 58, 23); final Button button_1 = new Button(shell, SWT.NONE);
button_1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {]
   //这里需要写停止的事件程序代码
}
});
button_1.setText("停止");
button_1.setBounds(313, 130, 44, 23);
//
}

解决方案 »

  1.   

    定义类成员变量.private boolean isRunning = true;
    while(isRunning) {
       ..//你的循环
    }
    button_1.addSelectionListener(new SelectionAdapter() { 
    public void widgetSelected(final SelectionEvent e) {] 
      isRunning = false; 

    }); 
      

  2.   

    shiyongxiancheng使用线程么,阻止线程么,
      

  3.   

    你程序死掉是由于你循环一直占用cpu,最好做while循环的时候每次结束sleep0.5秒左右.这样你的后续操作才能成功.
      

  4.   

    不知道你是怎么加的 像这类情况就是要在响应方法中另开线程处理public void widgetSelected(final SelectionEvent e) {
        new Thread() {
    public void run() {
        isStop = false;
        while (!isStop) {
            System.out.println("go");
    // try {
    //     Thread.sleep(1000);
    // } catch (InterruptedException e1) {
    //     // TODO Auto-generated catch block
    //     e1.printStackTrace();
    // }
        }
    }
        }.start();
    }并通过测试
      

  5.   

    给你参考,直接运行SWTStopThread
    package csdn;import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;public class SWTStopThread {
    static boolean resume = false;
    public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout()); final SWTSearchThread searchThread = new SWTSearchThread();
    final Button startBtn = new Button(shell, SWT.PUSH);
    startBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    startBtn.setText("Start Search");
    startBtn.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
    if (resume) {
    searchThread.resume();
    } else {
    searchThread.start();
    } }
    }); final Button stopBtn = new Button(shell, SWT.PUSH);
    stopBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    stopBtn.setText("Stop Search");
    stopBtn.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
    searchThread.suspend();
    resume = true;
    }
    });
    shell.setVisible(true);
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    searchThread.stopWork();
    display.dispose();
    }
    }/*
     * $RCSfile: SAWorkThread.java,v $
     * $Revision: 1.1 $
     * $Date: 2008/05/07 05:54:09 $
     * $Author: SYSO\xuqingkang $
     * Revison: 1.0
     * History : 2008/04/24 (xuqk) 1.0 Create
     *
     * Copyright 2008 NEC Corp. All Rights Reserved.
     */
    package csdn;
    /**
     * TODO To change the template for this generated type comment go to Window -
     * Preferences - Java - Code Style - Code Templates
     */
    public class SWTSearchThread extends Thread { private boolean stoped = false; private int count = 0; public void run() {
    try {
    while (!stoped) {
    System.out.println("I am searching."+(count++));
    }
    } catch (Throwable ex) {
    } finally {
    }
    } public void stopWork() {
    stoped = true;
    } /**
     * @return Returns the stoped.
     */
    public boolean isStoped() {
    return stoped;
    }
    }