Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 关于swt多线程的问题
123qwe





发贴: 1
积分: 0
于 2007-10-25 10:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我要实现的***能如下,窗口中有一个list,一个button,点button则调用可执行程序cmd,同时在list中输出cmd运行时的信息。cmd运行结束且信息输出完后判断cmd的exitValue,看是否正常结束。我用timer来实现,但是我发现问题很多:1、我如何判断信息输出完成呢?我现在是判断读buffer,如果读出空行就判断输出完成,但是如果cmd的输出信息的间隔时间很长肯定有问题。2、为什么我在主程序里加了while就一直循环不走timer了?是因为timer的runable启的不是线程吗? 3、有没有别的好方法能够实现我所需要的***能 呢?

public class window {

  private List list;
  
  protected Shell shell;
  private int i=0;

  /**
   * Launch the application
   * @param args
   */
  public static void main(String[] args) {
    try {
      window window = new window();
      window.open();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Open the window
   */
  public void open() {
    final Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
  }

  /**
   * Create contents of the window
   */
  protected void createContents() {
    shell = new Shell();
    shell.setSize(500, 375);
    shell.setText("SWT Application");

    final Button button = new Button(shell, SWT.NONE);
    button.setText("button");
    button.setBounds(197, 269, 58, 29);
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        BufferedReader buff2;
        int f=0;
        
        try{
          Process proess = Runtime.getRuntime().exec(Command);
InputStreamReader Inps = new InputStreamReader(process.getInputStream());
          buff2 = new BufferedReader(Inps);
          
          Timer timer = new Timer();
          Task task = new Task(buff2,list);
          timer.schedule(task,0,100);
          
          
          while((f=task.getInt())==0)
         { }
         if(f==2){timer.cancel();
if(proces.exitValue() != 0){
System.out.println("cmd error");
}        
        }catch(IOException ioe){}                
      }
    });

    list = new List(shell, SWT.V_SCROLL | SWT.BORDER);
    list.setBounds(108, 31, 265, 100);
  }
  
}
        
class Task extends TimerTask {
  private int j=0;
  private BufferedReader buff;
  private String ReturnMess = " ";
  private List list;

  public Task(BufferedReader buff1, List list1){
    buff = buff1;
    list = list1;
  }
  
  public int getInt(){
    return j;
  }
  
  public void run() {
    Display aa = Display.getDefault();
    aa.asyncExec(new Runnable() {
      public void run(){
        try{
          if ((ReturnMess= buff.readLine())!=null) {
            list.add(ReturnMess);
          }
          else{
            j=2;
            System.out.println("j="+j);
          }
        }catch(IOException e){}
      }      
    });
  }
}




话题树型展开
人气 标题 作者 字数 发贴时间
4837 关于swt多线程的问题 123qwe 2718 2007-10-25 10:22

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923