Question

I have a loop doing like this :

while (!exit){

read/write on file

}

And I have a button on my frame which his action performed change the value of "exit", the exit condition of the loop.

My problem, I am stuck in my loop, I can't click on my button because the program is all the time in the loop I think.

Someone told me to see with the Thread with the "polling" but I don't understand how i can integrate in my loop ?

EDIT :

How I call the 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;
}
Was it helpful?

Solution

Because read/write operation could be a really long task you should execute it in a different thread to not block the main UI thread.

SwingWorker class can be helpful, because it's created to handle such long-time tasks. Have a look here: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

If you use SwingWorker than instead of your flag you should call cancel method of SwingWorker when someone will press your button.

OTHER TIPS

You are probably running the loop in GUI thread, which is also responsible for handling the button click. While it is stuck in while loop, it's not available for changing value of variable 'exit'. Do the reading/writing in separate thread, there is a convenience class for it in Swing:

SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
// IO stuff
            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top