123qwe
发贴: 1
积分: 0
|
于 2007-10-25 10:22
我要实现的***能如下,窗口中有一个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){} } }); } }
|