1

I have a client <-> server application. The problem is when I click a button "Exit" it does not kill the entire thread. It kills the process, but the thread still remains active. But if I click the "X" button the JOptionPane.showConfirmDialog is stopping everything, so I would like the button to do the same (to be terminated).

P.P. The "Exit" button starts a JSwingWorker which is calling disconnect()

@Override
public void run() {
    synchronized (this) {
        while (!serverDownAfterTry && !serverClosedByButton) {
            multicastSocket = connectToMulticastAddress(multicastIP);
            mediator = new Mediator(multicastSocket, this, objectOutput, userName);
            mediator.setSocketIP(clientSocket.getInetAddress().toString());
            mediator.setSocketPort(clientSocket.getPort());
            String input = "";
            if (connected) {
                mediator.writeOnMulticastAddress("USER_CONNECTED " + this.userName);
                mediator.executeCommands("SYSTEM_MESSAGE " + this.userName, clientGUI);
                input = mediator.listenToMultiCast(multicastSocket);
            }
            while (!multicastSocket.isClosed()) {
                input = mediator.listenToMultiCast(multicastSocket);
                if (input != null) {
                    mediator.executeCommands(input, clientGUI);
                }
            }
        }
    }
}

public void disconnect() {
    clientGUI.dispose();
    serverClosedByButton = true;
    multicastSocket.close();
    try {
        objectOutput.close();
        clientSocket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

} This is what happens when i click Exit button: enter image description here

This is what happens when I click "X" button on JFrame(terminated): enter image description here

4
  • where is this serverClosedByButton declared? Commented Aug 18, 2017 at 11:56
  • When you use Swing, you need to terminate your VM to exit. For example by using of System.exit(0);. Commented Aug 18, 2017 at 12:19
  • ΦXocę 웃 Пepeúpa ツ the whole code is 300 lines long, I posted what is neccessery :) They are private declared. It exits from the loop, but does not terminate. I will try System.exit now Commented Aug 18, 2017 at 12:54
  • @SergiyMedvynskyy yes it worked ! You can post this as a new answer, so I can mark it as answer :) Thank you ! Commented Aug 18, 2017 at 12:56

1 Answer 1

1

When you use Swing, you need to terminate your VM to exit. For example by using of System.exit(0);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.