新手关于SWT UI线程与非UI线程实现信息交互的问题
最近,在公司开发一插件,要求发送指令连接硬件,然后得到连接信息(无论正确与否),返回显示在一个Text中
因为刚出来实习,第一次做项目而且是在不怎么熟悉的linux下,有点手忙脚乱,
执行的结果是第一次能读取,但是不能再次读取,求大神指点迷津
connect是第一次操作,send是第2次操作;
代码如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.internal.handlers.WizardHandler.New;
public class DownloadView extends Dialog{
private String message;
private Process command;
private Text text;
private Text address_t;
private String exec_f;
private String down_f;
private String address;
private Button discButton;
private InputStream i;
private BufferedReader reader;
private boolean flag=false;
String str;
static String s1;
private int c;
private int c1;
InputStreamReader reader1;
public DownloadView(Shell parentShell) {
super(parentShell);
}
protected void createButtonsForButtonBar(Composite parent) {
//重写父类方法,取消自带的OK cancel按钮
}
//重写父类方法,定义自己的组件
protected Control createDialogArea(Composite parent) {
this.getShell().setText("JLINK");
Composite com=new Composite(parent, SWT.None);
GridLayout lay=new GridLayout();
lay.numColumns = 1;
setMyLayout(lay);
com.setLayout(lay);
com.setLayoutData(new GridData(GridData.FILL_BOTH));
Composite composite=new Composite(com, SWT.None);
GridLayout layout=new GridLayout();
layout.numColumns = 3;
setMyLayout(layout);
composite.setLayout(layout);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
//连接按钮的处理
Button ConnectButton =new Button(composite, SWT.None);
ConnectButton.setText("CONNECT");
ConnectButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
message="sudo ejtag";
discButton.setEnabled(true);
doIt(message);
new MyThread().start();
System.out.println("线程已经启动");
System.out.println(message);
}
});
//断开连接按钮的处理
discButton =new Button(composite, SWT.None);
discButton.setText("DISCONNECT");
discButton.setEnabled(false);
discButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e){
message="x";
discButton.setEnabled(false);
doIt(message);
System.out.println(message);
}
});
//关闭窗口按钮的处理
Button closeButton=new Button(composite, SWT.None);
closeButton.setText("CLOSE");
closeButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e){
close();
}
});
Text but1 = null;
Text but2 = null;
Text but3 = null;
Composite composite2 = new Composite(com, SWT.NONE);
GridLayout layout2 = new GridLayout();
layout2.numColumns = 3;
composite2.setLayout(layout2);
composite2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
createOpenFile("TYPE:",but1,composite,false);
createOpenFile("FILE:", but2, composite,true);
new Label(composite, SWT.NONE).setText("ADDR:");
but3 = new Text(composite, SWT.NONE);
address_t = but3;
//SEND 按钮的处理
Button sendButton=new Button(composite, SWT.None);
sendButton.setText("SEND");
sendButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
try {
command=Runtime.getRuntime().exec("sudo mips-gdb");
new MyThread().start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
but3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Composite composite3=new Composite(com, SWT.None);
text = new Text(composite3, SWT.WRAP|SWT.V_SCROLL);
text.setEditable(false);
text.setBounds(0, 0, 550, 200);
composite3.layout();
applyDialogFont(composite);
return composite;
}
private void createOpenFile(final String name,Text t,Composite composite,Boolean s){
//GridData date = new GridData();
new Label(composite,SWT.NONE).setText(name);
t = new Text(composite,SWT.NONE);
if(name.equals("command:")){
if(exec_f!=null){
t.setText(exec_f);
}
else
t.setText("/usr/bin/jlink");
}
t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final Text tt = t;
Button b = new Button(composite,SWT.NONE);
b.setText("设置");
b.setVisible(s);
b.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
FileDialog f = new FileDialog(getShell());
String path = f.open();
tt.setText(path);
if(name.equals("command:"))
{
exec_f = "sudo " + tt.getText() + " -w ";
// System.out.println(exec_f);
}
else
{
down_f = tt.getText();
//System.out.println(down_f);
}
}
});
}
private void doIt(String s){
try {
if (s!=null&&s.equals("")!=true) {
command=Runtime.getRuntime().exec(s);
}
}catch (IOException e) {
e.printStackTrace();
}
}
private void setMyLayout(GridLayout layout){
layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
}
class MyThread extends Thread{
public void getStream(InputStream stream,String mes) throws IOException {
if ((c=stream.read())!=-1) {
i=stream;
System.out.println(mes);
}else {
stream.close();
}
}
private void printIt(BufferedReader reader) {
try{
System.out.println("获取流成功");
System.out.println(reader.ready()+"________________");
String st=reader.readLine();
byte []a=new byte[]{(byte)c,(byte)c1};//拼接因为read()方法而丢失的字节
str=new String(a,"UTF-8")+st+"\n";
System.out.println(str);
getIt(str);
int c2=0;
char []arr=new char[512];
command=null;
System.out.println("My command is "+command);
while ((c2=reader.read(arr, 0, 512))!=-1) {
System.out.println("已读一行");
s1=new String(arr,0,c2);
System.out.println(s1);
getIt(s1);
}
System.out.println("打印完毕");
}catch (IOException e) {
getIt(e.getMessage());
}finally{
try {
reader.close();
System.out.println("缓冲流已经关闭");
//i.close();
// System.out.println("字节流已经关闭");
} catch (IOException e) {
// TODO Auto-generated catch block
getIt(e.getMessage());
}
}
}
public void run() {
// synchronized (this) {
try {
while(command!=null) {
System.out.println("进入run");
getStream(command.getErrorStream(),"error流");
if((c1=i.read())==-1){
getStream(command.getInputStream(),"input流");
}else{
System.out.println("流建立完成");
reader=new BufferedReader(new InputStreamReader(i));
printIt(reader);
}
}
} catch (IOException e) {
getIt(e.getMessage());
}finally{
System.out.println("11111111");
}
// }
}
public void getIt(final String string){
text.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
text.append(string);
}
});
}
}
}