Javafx GUI使用无限循环挂起

时间:2019-05-03 06:23:43

标签: java javafx arduino rfid

我有fetch函数,该函数从arduino中获取数据,在此函数中,我使用无限while循环,并且我希望在按GUI中的单选按钮时退出while循环,但是我无法按由于while循环导致GUI挂起,导致单选按钮。

我已经尽了最大的努力,并且达到了最终代码。

            @FXML
          void doFetch(ActionEvent event) throws IOException, 
         InterruptedException {

        while(true){
            OutputStream os=s1.getOutputStream();
            String messageString1 ="^"; 

            //Fetching input key
            String messageString2="";

                    messageString2 = txtKey1.getText();


            // decoding the key
             String aa2[]=messageString2.split(" ");

            int dec2[]=new int[6];
            String c2="";
            for(int i=0;i<6;i++)
            {
                dec2[i]=Integer.parseInt(aa2[i],16);
                c2=c2.concat(String.valueOf(dec2[i]));
                if(i != 5)
                {
                c2=c2.concat(" ");
                }
            }

            // concatenating the whole string
            String messageString=c2;
            messageString=messageString.concat(")");
            messageString=messageString.concat(messageString1);
            System.out.println(messageString);
            os.write(messageString.getBytes());

            os.flush();
            os.close();

            Thread.sleep(2000);

            // sending string to serial monitor

           scan();// receiving data from arduino
           try {
                Thread.sleep(5000);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           InputStream is1=s1.getInputStream();
            //doAlert("Block written successfully !");
            for(int i=0;i<20;i++){
                Thread.sleep(5000);
                System.out.println("saa");
                if((char)is1.read()=='-')
                break;
            }



          try
          {
            if(txtStop.isSelected()==true)
                break;
            else if(txtStop1.isSelected()==true)

                break;  
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
         }

想要在循环中设置ID = txtStop的停止按钮时中断此操作

0 个答案:

没有答案
相关问题