繁体   English   中英

使用actionListener()退出InputStream.read()

[英]Quit InputStream.read() with an actionListener()

我有这样一个循环:

while(sortie == false) {
     int availableBytes = 0;
     try {
            read = 0;
            availableBytes = inputStream.available();
            if (availableBytes > 0) {
            System.out.println("je suis dans le availableBytes > 0 du while -- read = "+read);                                          read = read + availableBytes;

            int raw = inputStream.read(readBuffer, read-availableBytes, availableBytes);
            traduction = new String(readBuffer, read-availableBytes, raw);
            System.out.println("2=>" + traduction);
            tradV2 = tradV2 + traduction;    // bytes -> String
        }
    if (read == 19){
            System.out.println("une donnee de 19 char lue -- read = "+read);
        }
    } catch (IOException e) {
    }

}

在调用此命令之前,我先进行初始化工作并设置端口参数。

我有一个这样声明的JButton操作侦听器:

JB_MeasurMode.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent arg0) {
                    sortie = true;
                }      
            });

因此,我要尝试执行的操作将进入“测量模式”,这意味着程序将保持对端口com的读取模式。 为了退出“测量模式”,我添加了一个jbutton,它应该更改while条件以退出它。 但是我的问题是,当我进入while循环时,我所有的按钮都是“冻结的”,我无法单击。

当我执行“ InputStream.Read”时,它会阻止所有其他事件吗?

您使用“ while”循环执行的操作称为“轮询”。

http://en.wikipedia.org/wiki/Polling_%28computer_science%29

我建议您实施应同时实施的解决方案。

1- Thread.currentThread()。sleep(millis) -循环结束时

2-在单独的线程中启动循环,请记住安全线程布尔变量,例如java.util.concurrent.atomic.AtomicBoolean

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM