Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Thread sleeping and not blocking the GUI  (Read 1051 times)
0 Members and 1 Guest are viewing this topic.
Offline Rockslide

Junior Newbie





« Posted 2011-03-06 13:59:36 »

Hi,
Please help me out with the following Smiley

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
    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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
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
Offline zoto

Senior Member


Medals: 4



« Reply #1 - Posted 2011-03-06 14:11:21 »

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
Offline Rockslide

Junior Newbie





« Reply #2 - Posted 2011-03-06 14:52:39 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #3 - Posted 2011-03-06 15:05:16 »

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.
Offline Rockslide

Junior Newbie





« Reply #4 - Posted 2011-03-06 15:28:40 »

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:

1  
2  
3  
        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
Offline princec

JGO Kernel


Medals: 339
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2011-03-06 15:31:38 »

Yeah it's a problem about you not understanding threads Smiley 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 Smiley

Offline Rockslide

Junior Newbie





« Reply #6 - Posted 2011-03-06 17:54:53 »

Yeah it's a problem about you not understanding threads Smiley 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 Smiley

Thanks, I guess I assumed wrong Smiley
I did what you said and it works now.
Offline princec

JGO Kernel


Medals: 339
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2011-03-06 18:15:12 »

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

Cas Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (32 views)
2014-07-18 06:55:21

Zero Volt (28 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (25 views)
2014-07-16 23:30:00

Cero (40 views)
2014-07-16 00:42:17

Riven (42 views)
2014-07-14 18:02:53

OpenGLShaders (29 views)
2014-07-14 16:23:47

Riven (29 views)
2014-07-14 11:51:35

quew8 (26 views)
2014-07-13 13:57:52

SHC (63 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!