繁体   English   中英

在外部事件时退出while循环

[英]Quit while loop with external event

我有这样一个循环:

while (!exit){

read/write on file

}

我的框架上有一个按钮,他执行的动作更改了循环的退出条件“ exit”的值。

我的问题是,我陷入了循环,无法单击按钮,因为我认为程序始终处于循环状态。

有人告诉我使用带有“轮询”的线程,但是我不知道如何将其集成到循环中?

编辑:

我如何称呼Thread:

case "Mode Measure": {
    JOptionPane.showMessageDialog(null,"Radar in Measure Mode : ON ");
    System.out.println("READ Mode : ON ");
    JB_MeasurMode.setVisible(true);

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

    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        CommPortIdentifier portId = null;
        try {
            portId = CommPortIdentifier.getPortIdentifier(ChoixPortCom);
            serialPort = (SerialPort) portId.open("Main Frame", 5000);
            System.out.println("serialPort ouvert");
            outputStream = serialPort.getOutputStream();
            inputStream = serialPort.getInputStream();
            serialPort.setRTS(false);
            serialPort.setInputBufferSize(8192);
            serialPort.setOutputBufferSize(8192);
            serialPort.setSerialPortParams(115400,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN |SerialPort.FLOWCONTROL_XONXOFF_OUT);
            catch (NoSuchPortException e1) {
                // TODO Auto-generated catch block
                    e1.printStackTrace();
            } catch (PortInUseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnsupportedCommOperationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    System.out.println("Dans le while");
    while(sortie == false) {
            int i = 0;
        int availableBytes = 0;
        try {
            int read = 0;
            availableBytes = inputStream.available();
            if (availableBytes > 0) {
                String tradV3 = null;
                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);
                tradV3 = tradV3 + traduction;   // bytes -> String
            }

            if (read == 19){

                System.out.println("une donnee de 19 char lue -- read = "+read);
                System.out.println("Non-Traité :"+Arrays.toString(readBuffer));
                int[] tab_int2 = new int[19];
                tab_int2 = Fonction.Get_Msg_19(readBuffer);
                String Msg_affiche2 = Arrays.toString(tab_int2);
                System.out.println("Traité : "+Msg_affiche2);
                Measure t = new Measure();
                t.GetMeasure(tab_int2);
                Tab_Measure[i] = t;
                i++;
            }
            } catch (IOException e) {
                System.out.println("Problem avec le try !!!");
            }
        }
        System.out.println("Sortie du while");
                }
    });
break;
}

因为读/写操作可能是一个很长的任务,所以您应该在另一个线程中执行它,以免阻塞主UI线程。

SwingWorker类可能会有所帮助,因为它是为处理此类长期任务而创建的。 在这里看看: http : //docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

如果使用SwingWorker而不是标志,则当有人按下您的按钮时,应调用SwingWorker的cancel方法。

您可能正在GUI线程中运行循环,它也负责处理按钮单击。 虽然卡在while循环中,但不可用于更改变量“ exit”的值。 在单独的线程中进行读取/写入,Swing中提供了一个便利类:

SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
// IO stuff
            }
        });

暂无
暂无

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

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