Thread: [Java-gnome-developer] Problem with Threading (I guess)
Brought to you by:
afcowie
From: Stefan P. <pr...@tz...> - 2006-09-17 13:55:30
|
Hi all, I am trying to build a splashscreen that shows the process while the application is loading data. I found that after a short time, no progress changes are visible while the loading continues. I broke my problems down to a simple test program that forks a thread to change the progress and that waits a specific amount of time between two changes. public class SplashTest { static ProgressBar bar; static TextView text; //------------------------------------------------------ public static void main(String[] args) { Gtk.init(args); // Open a window with a progress bar and a text view. Window window = new Window(); VBox box = new VBox(false,5); window.add(box); bar = new ProgressBar(); box.add(bar); text = new TextView(); box.add(text); window.showAll(); // Now create progress changing thread Thread thread = new Thread(new Runnable(){ public void run() { for (int i=0; i<=100; i+=10) { System.out.println("Now "+i); bar.setFraction(i/100.0); bar.setText(i+"%"); text.getBuffer().setText("Loaded "+i+"%"); synchronized(this) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}); thread.start(); Gtk.main(); } } When I do run this program, the progress bar stops at 10 or 20 percent, the text view stops at 0 or 10 percent. If you reduce the waiting time between changes to a lower value - e.g. 500ms - everything is fine. Any idea what is causing this? Regards, Stefan |
From: Emmanuel R. <emm...@gm...> - 2006-09-17 21:00:03
|
Hi, I think that the problem is because you are accessing your widgets from your second thread. Only the event loop thread is allowed to manipulate the widgets. Take a look at the following page there is an explanation and some sample code. http://java-gnome.sourceforge.net/cgi-bin/bin/view/Main/MultiThreaded Stefan Prelle wrote: > Hi all, > > I am trying to build a splashscreen that shows the process while the > application is loading data. I found that after a short time, no > progress changes are visible while the loading continues. > > I broke my problems down to a simple test program that forks a thread to > change the progress and that waits a specific amount of time between two > changes. > > public class SplashTest { > > static ProgressBar bar; > static TextView text; > > //------------------------------------------------------ > public static void main(String[] args) { > Gtk.init(args); > > // Open a window with a progress bar and a text view. > Window window = new Window(); > VBox box = new VBox(false,5); > window.add(box); > bar = new ProgressBar(); > box.add(bar); > text = new TextView(); > box.add(text); > window.showAll(); > > // Now create progress changing thread > Thread thread = new Thread(new Runnable(){ > public void run() { > for (int i=0; i<=100; i+=10) { > System.out.println("Now "+i); > bar.setFraction(i/100.0); > bar.setText(i+"%"); > text.getBuffer().setText("Loaded "+i+"%"); > synchronized(this) { > try { > Thread.sleep(1000); > } catch (InterruptedException e) { > // TODO Auto-generated catch block > e.printStackTrace(); > } > } > } > }}); > > thread.start(); > Gtk.main(); > } > } > > When I do run this program, the progress bar stops at 10 or 20 percent, > the text view stops at 0 or 10 percent. > If you reduce the waiting time between changes to a lower value - e.g. > 500ms - everything is fine. > > Any idea what is causing this? > > Regards, > Stefan > > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > java-gnome-developer mailing list > jav...@li... > https://lists.sourceforge.net/lists/listinfo/java-gnome-developer > > |
From: Andrew C. <an...@op...> - 2006-09-18 07:58:44
|
On Sun, 2006-09-17 at 15:54 +0200, Stefan Prelle wrote: > Hi all, >=20 > I am trying to build a splashscreen that shows the process while the > application is loading data. I found that after a short time, no > progress changes are visible while the loading continues. Worker thread. http://research.operationaldynamics.com/projects/examples/dist/worker-4.tar= .gz [Incidentally, the events bug that the web page Emmanuel pointed at is long gone from java-gnome. We really need to rectify or ditch that wiki] [Also, I personally don't much care for the way we handle this problem - ie the CustomEvents(Runable) technique - but there won't be an appreciable change unless we make a significant architectural shift. Fingers crossed we can figure out a way to do it] AfC Sydney --=20 Andrew Frederick Cowie Technology strategy, managing change, establishing procedures, and executing successful upgrades to mission critical business infrastructure. http://www.operationaldynamics.com/ Sydney New York Toronto London |