Thread sleeping and not blocking the GUI

Hi,
Please help me out with the following :slight_smile:

Situation:
A login screen with only a “connect” button. Clicking the button should make the game connect to a server.
The server is not running, so it should just keep trying to connect (even though it never will) while not blocking the GUI.

Problem:
GUI freezes when I click the button. I have to kill the process with traskmanager.

Code:
This is the Thread that starts when the button is clicked:

    private static class ButtonCallback implements Runnable {
    	Game game;
    	public ButtonCallback(Game game) {
    		this.game = game;
    	}
    	
    	public void run() {
    		game.network.connect("XXX.XXX.XXX.XXX", XXXX); // edited out IP for privacy
    	}
    }

This is the class containing the ‘connect()’ method:

public class Network {
	Game game;
	SocketChannel socketChan;
	
	public Network(Game game) {
		this.game = game;
		socketChan = null;
	}
	
	public void connect(String server, int port) {
		game.loginGui.lblStatus.setText("Connecting to: " + server + ":" + Integer.toString(port));
		try {
			socketChan = SocketChannel.open();
			socketChan.configureBlocking(false);
			socketChan.connect(new InetSocketAddress(server, port));
			while (socketChan.isConnectionPending()) {
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {}
			}
			game.loginGui.lblStatus.setText("Connected.");
		} catch (UnknownHostException e) {
			game.loginGui.lblStatus.setText("ERROR: Unknown host");
			e.printStackTrace();
		} catch (IOException e) {
			game.loginGui.lblStatus.setText("ERROR: Failed to connect!");
			e.printStackTrace();
		}
	}
}

I thought the Thread.sleep would make sure the GUI keeps working. Why doesn’t it?

Additional info:
Using LWJGL and TWL
Java eclipse
Windows 7

You will need the work performed in a different thread than the GUI.
This link should help http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

Yes, but isn’t that what I’m doing by implementing runnable?

Implementing Runnable is necessary but not sufficient. I don’t see any reference of ButtonCallback in the main code…

Anyways, the link posted by zoto contains the information you need.

That link describes how it works in Swing, but I’m not using Swing, I’m using TWL.

The ButtonCallback is refered to in the main widget’s constructor. Like so:

        btnConnect = new Button("Connect");
        btnConnect.addCallback(new ButtonCallback(this.game));
        add(btnConnect);

I assume TWL runs the runnable ‘ButtonCallback’ as a new thread when the button is clicked. (And indeed my run() code gets executed when I click the button).

Is this a problem of me not understanding threads, or is it specific to TWL and should I ask this question on a TWL forum?

Thanks

Yeah it’s a problem about you not understanding threads :slight_smile: The callback will be performed in the same thread as the event is fired. So your callback actually needs to spawn a new Thread to do the actual asychronous work.

Cas :slight_smile:

Thanks, I guess I assumed wrong :slight_smile:
I did what you said and it works now.

Don’t forget to disable the button before the thread starts, and then enable it again when the thread finishes.

Cas :slight_smile: