简体   繁体   English

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

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

I have a loop like this : 我有这样一个循环:

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) {
    }

} }

Before calling this, I do the initialization stuff and setting port parameters. 在调用此命令之前,我先进行初始化工作并设置端口参数。

And I have a JButton action listener declared like this : 我有一个这样声明的JButton操作侦听器:

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

So what I'm trying to do its, it enters in "Measure Mode", that means the program stays in read mode to the port com. 因此,我要尝试执行的操作将进入“测量模式”,这意味着程序将保持对端口com的读取模式。 And to quit this "Measure Mode" ,I added an jbutton which is supposed to change the while condition to exit it. 为了退出“测量模式”,我添加了一个jbutton,它应该更改while条件以退出它。 But my problem, when I enter in the while loop, all my button are "frozen", I can't click on. 但是我的问题是,当我进入while循环时,我所有的按钮都是“冻结的”,我无法单击。

When I do the "InputStream.Read" does'n it block all other events ? 当我执行“ InputStream.Read”时,它会阻止所有其他事件吗?

What you are doing with "while"-loop is called POLLING. 您使用“ while”循环执行的操作称为“轮询”。

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

I suggest you to implement solutions which should be implemented simultaneously. 我建议您实施应同时实施的解决方案。

1 - Thread.currentThread().sleep( millis ) - at the end of the loop 1- Thread.currentThread()。sleep(millis) -循环结束时

2 - start the loop in seperate thread, remember of safe-thread boolean variable, like java.util.concurrent.atomic.AtomicBoolean 2-在单独的线程中启动循环,请记住安全线程布尔变量,例如java.util.concurrent.atomic.AtomicBoolean

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

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