Home » Eclipse Projects » Standard Widget Toolkit (SWT) » GUI hanging.
GUI hanging. [message #447271] |
Sun, 12 December 2004 16:01 |
Matt McKenzie Messages: 2 Registered: July 2009 |
Junior Member |
|
|
--____LPHMXLZMXOMRLFKSEJCW____
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Hi
I am new to java programing and even newer to SWT programming. I have =
found a problem where the GUI interface will "Hang". The background =
processes are all working but the GUI stops updating. (Running in a =
Windows XP environment)
I do not know if this is bug or a problem with my code. Below is a sample =
program that demonstrates the problem.
The program outputs a number to a text box and at the same time printing =
it to the system console. It is in a simple for loop. When the program is =
running press in quick succession the mouse about 10 to 20 times, The GUI =
will hang, however the System console is still being updated.
Is this a bug, or is their another way to do this.
I would be most gratefull for any assistance.
Thanks
Matt
Sample program
public class Temp {
public static void main(String[] args) {
Temp t =3D new Temp();
t.start();
}
public void start(){
Display display =3D new Display();
Shell shell =3D new Shell(display);
Text text =3D new Text(shell,SWT.MULTI);
text.setSize(200,200);
text.setEditable(false);
shell.open();
=20
int y=3D1000000;
for (int x=3D0;x<y;x++){
text.append("\nCounter.. "+x);
System.out.print("\nCounter.. "+x);
}
=20
shell.open();
while (!shell.isDisposed()){
if (!display.readAndDispatch())display.sleep();
}
display.dispose();
}
}
--____LPHMXLZMXOMRLFKSEJCW____
Content-Type: multipart/related; boundary="____WHPEPQYSAQXEHDGESJXG____"
--____WHPEPQYSAQXEHDGESJXG____
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Diso-8859-1"=
>
<META content=3D"MSHTML 6.00.2800.1400" name=3DGENERATOR></HEAD>
<BODY style=3D"MARGIN: 4px 4px 1px; FONT: 10pt Tahoma">
<DIV>Hi</DIV>
<DIV> </DIV>
<DIV>I am new to java programing and even newer to SWT programming. I have =
found a problem where the GUI interface will "Hang". The background =
processes are all working but the GUI stops updating. (Running in a =
Windows XP environment)</DIV>
<DIV> </DIV>
<DIV>I do not know if this is bug or a problem with my code. Below is a =
sample program that demonstrates the problem.</DIV>
<DIV> </DIV>
<DIV>The program outputs a number to a text box and at the same time =
printing it to the system console. It is in a simple for loop. When the =
program is running press in quick succession the mouse about 10 to 20 =
times, The GUI will hang, however the System console is still being =
updated.</DIV>
<DIV> </DIV>
<DIV>Is this a bug, or is their another way to do this.</DIV>
<DIV> </DIV>
<DIV>I would be most gratefull for any assistance.</DIV>
<DIV> </DIV>
<DIV>Thanks</DIV>
<DIV> </DIV>
<DIV>Matt</DIV>
<DIV> </DIV>
<DIV>Sample program</DIV>
<DIV> </DIV>
<DIV>public class Temp {</DIV>
<DIV> </DIV>
<DIV> public static void main(String[] args) {<BR> Temp t =
=3D new Temp();<BR> t.start();<BR> } <BR> public void =
start(){<BR> Display display =3D new Display();<BR> S=
hell shell =3D new Shell(display);<BR> Text text =3D new =
Text(shell,SWT.MULTI);<BR> text.setSize(200,200); <BR>  =
;text.setEditable(false);<BR> shell.open(); <BR> <BR>&=
nbsp; int y=3D1000000;<BR> for (int x=3D0;x<y;x++){<BR>&=
nbsp; text.append("\nCounter.. "+x);<BR> =
System.out.print("\nCounter.. "+x);<BR> }<BR> <=
BR> shell.open();<BR> while (!shell.isDisposed()){<BR=
> if (!display.readAndDispatch())display.sleep();<BR> =
; }<BR> display.dispose();<BR > }<BR>}<BR></DIV></BODY>=
</HTML>
--____WHPEPQYSAQXEHDGESJXG____--
--____LPHMXLZMXOMRLFKSEJCW____--
|
|
|
Re: GUI hanging. [message #447272 is a reply to message #447271] |
Sun, 12 December 2004 17:39 |
Mani Ghamari Messages: 33 Registered: July 2009 |
Member |
|
|
Hi Matt,
Your "for" loop is a long-running process.
When you start clicking around while the loop is running, the mouse events
are queued directly in the SWT event queue, which must be processed by the
UI-Thread BEFORE any other GUI operations can be processed (e.g.
Text.append(String str) ).
So, the mouse events block the event queue, because the the event processing
code
(generally: display.readAndDispatch() never gets a chance to process and
execute (or in SWT terms, read and dispatch!) the queued events. (because
the programm just "STAYS" in your for loop, the whole time).
Here is a possible solution to the problem which I use often myself: I put
the update loop in a seperate Thread, so that the UI-Thread can process the
events without getting blocked. Note that display.syncExec() is used to pass
the UI-changing instructions back to the UI-Thread...
For more info, take a look at the API docs for:
org.eclipse.swt.widgets.Display
&
Display.syncExec(Runnable runnable)
Display.asyncExec(Runnable runnable)
you might also want to take a look at the articles found on these pages,
which both contain excellent tutorials for starters...:
http://www.eclipse.org/articles
http://www.cs.umanitoba.ca/~eclipse/
/* ***************** here is code********* */
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
public class Temp implements Runnable
{
// display and text are made global so that the references are available
within the run() method
Display display;
Text text;
// running is made global because otherwise it has to be declared final
to be accessible
// from the inner class
boolean running;
public void run()
{
int y=1000000;
// if running is false, the shell is disposed and the thread should
stop running
// the variable running is used to prevent an Text.append(String
str), after the text is
// disposed (otherwise .append() would throw a
WidgetDisposedException)
for (int x=0;x<y && running;x++)
{
final int x2 = x;
// All calls to the GUI must be made within the UI-thread
display.syncExec(new Runnable()
{
public void run()
{
if (!text.isDisposed())
{
// shell and text are still there: update
text.append("\nCounter.. "+x2);
System.out.print("\nCounter.. "+x2);
}
else
{
// shell and text are disposed: stop running...
running = false;
}
}
});
}
}
public static void main(String[] args)
{
Temp t = new Temp();
t.start();
}
public void start()
{
running = true;
display = new Display();
Shell shell = new Shell(display);
text = new Text(shell,SWT.MULTI);
text.setSize(200,200);
text.setEditable(false);
shell.open();
// shell is created, start the update thread...
Thread thread = new Thread(this);
thread.start();
// you dont need to open the shell twice...
// shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())display.sleep();
}
display.dispose();
}
}
regards,
Mani
"Matt McKenzie" <mmckenzie@novell.com> wrote in message
news:cphqhm$nt5$1@www.eclipse.org...
Hi
I am new to java programing and even newer to SWT programming. I have found
a problem where the GUI interface will "Hang". The background processes are
all working but the GUI stops updating. (Running in a Windows XP
environment)
I do not know if this is bug or a problem with my code. Below is a sample
program that demonstrates the problem.
The program outputs a number to a text box and at the same time printing it
to the system console. It is in a simple for loop. When the program is
running press in quick succession the mouse about 10 to 20 times, The GUI
will hang, however the System console is still being updated.
Is this a bug, or is their another way to do this.
I would be most gratefull for any assistance.
Thanks
Matt
Sample program
public class Temp {
public static void main(String[] args) {
Temp t = new Temp();
t.start();
}
public void start(){
Display display = new Display();
Shell shell = new Shell(display);
Text text = new Text(shell,SWT.MULTI);
text.setSize(200,200);
text.setEditable(false);
shell.open();
int y=1000000;
for (int x=0;x<y;x++){
text.append("\nCounter.. "+x);
System.out.print("\nCounter.. "+x);
}
shell.open();
while (!shell.isDisposed()){
if (!display.readAndDispatch())display.sleep();
}
display.dispose();
}
}
|
|
|
Re: GUI hanging. [message #447284 is a reply to message #447272] |
Mon, 13 December 2004 09:15 |
Matt McKenzie Messages: 2 Registered: July 2009 |
Junior Member |
|
|
--____LPHMXLZMXOMRLFKSEJCW____
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Hi Mani
Thanks for the tips.
I will give to try.
Thanks
Matt
>>> Mani Ghamari<mani.ghamari@linkast.com> 12/12/2004 17:39:32 >>>
Hi Matt,
Your "for" loop is a long-running process.
When you start clicking around while the loop is running, the mouse =
events=20
are queued directly in the SWT event queue, which must be processed by =
the=20
UI-Thread BEFORE any other GUI operations can be processed (e.g.=20
Text.append(String str) ).
So, the mouse events block the event queue, because the the event =
processing=20
code
(generally: display.readAndDispatch() never gets a chance to process =
and=20
execute (or in SWT terms, read and dispatch!) the queued events. =
(because=20
the programm just "STAYS" in your for loop, the whole time).
Here is a possible solution to the problem which I use often myself: I =
put=20
the update loop in a seperate Thread, so that the UI-Thread can process =
the=20
events without getting blocked. Note that display.syncExec() is used to =
pass=20
the UI-changing instructions back to the UI-Thread...
For more info, take a look at the API docs for:
org.eclipse.swt.widgets.Display
&
Display.syncExec(Runnable runnable)
Display.asyncExec(Runnable runnable)
you might also want to take a look at the articles found on these =
pages,=20
which both contain excellent tutorials for starters...:
http://www.eclipse.org/articles
http://www.cs.umanitoba.ca/~eclipse/
/* ***************** here is code********* */
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
public class Temp implements Runnable
{
// display and text are made global so that the references are =
available=20
within the run() method
Display display;
Text text;
// running is made global because otherwise it has to be declared =
final=20
to be accessible
// from the inner class
boolean running;
public void run()
{
int y=3D1000000;
// if running is false, the shell is disposed and the thread =
should=20
stop running
// the variable running is used to prevent an Text.append(String=20=
str), after the text is
// disposed (otherwise .append() would throw a=20
WidgetDisposedException)
for (int x=3D0;x<y && running;x++)
{
final int x2 =3D x;
// All calls to the GUI must be made within the UI-thread
display.syncExec(new Runnable()
{
public void run()
{
if (!text.isDisposed())
{
// shell and text are still there: update
text.append("\nCounter.. "+x2);
System.out.print("\nCounter.. "+x2);
}
else
{
// shell and text are disposed: stop running...
running =3D false;
}
}
});
}
}
public static void main(String[] args)
{
Temp t =3D new Temp();
t.start();
}
public void start()
{
running =3D true;
display =3D new Display();
Shell shell =3D new Shell(display);
text =3D new Text(shell,SWT.MULTI);
text.setSize(200,200);
text.setEditable(false);
shell.open();
// shell is created, start the update thread...
Thread thread =3D new Thread(this);
thread.start();
// you dont need to open the shell twice...
// shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())display.sleep();
}
display.dispose();
}
}
regards,
Mani
"Matt McKenzie" <mmckenzie@novell.com> wrote in message=20
news:cphqhm$nt5$1@www.eclipse.org...
Hi
I am new to java programing and even newer to SWT programming. I have =
found=20
a problem where the GUI interface will "Hang". The background processes =
are=20
all working but the GUI stops updating. (Running in a Windows XP=20
environment)
I do not know if this is bug or a problem with my code. Below is a =
sample=20
program that demonstrates the problem.
The program outputs a number to a text box and at the same time printing =
it=20
to the system console. It is in a simple for loop. When the program is=20
running press in quick succession the mouse about 10 to 20 times, The =
GUI=20
will hang, however the System console is still being updated.
Is this a bug, or is their another way to do this.
I would be most gratefull for any assistance.
Thanks
Matt
Sample program
public class Temp {
public static void main(String[] args) {
Temp t =3D new Temp();
t.start();
}
public void start(){
Display display =3D new Display();
Shell shell =3D new Shell(display);
Text text =3D new Text(shell,SWT.MULTI);
text.setSize(200,200);
text.setEditable(false);
shell.open();
int y=3D1000000;
for (int x=3D0;x<y;x++){
text.append("\nCounter.. "+x);
System.out.print("\nCounter.. "+x);
}
shell.open();
while (!shell.isDisposed()){
if (!display.readAndDispatch())display.sleep();
}
display.dispose();
}
}
--____LPHMXLZMXOMRLFKSEJCW____
Content-Type: multipart/related; boundary="____WHPEPQYSAQXEHDGESJXG____"
--____WHPEPQYSAQXEHDGESJXG____
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Diso-8859-1"=
>
<META content=3D"MSHTML 6.00.2900.2180" name=3DGENERATOR></HEAD>
<BODY style=3D"MARGIN: 4px 4px 1px; FONT: 10pt Tahoma">
<DIV>Hi Mani</DIV>
<DIV> </DIV>
<DIV>Thanks for the tips.</DIV>
<DIV> </DIV>
<DIV>I will give to try.</DIV>
<DIV> </DIV>
<DIV>Thanks</DIV>
<DIV> </DIV>
<DIV>Matt</DIV>
<DIV><BR>>>> Mani Ghamari<mani.ghamari@linkast.com> =
12/12/2004 17:39:32 >>><BR></DIV>
<DIV style=3D"COLOR: #000000">Hi Matt,<BR><BR>Your "for" loop is a =
long-running process.<BR><BR>When you start clicking around while the loop =
is running, the mouse events <BR>are queued directly in the SWT event =
queue, which must be processed by the <BR>UI-Thread BEFORE any other GUI =
operations can be processed (e.g. <BR>Text.append(String str) ).<BR><BR>So,=
the mouse events block the event queue, because the the event processing =
<BR>code<BR>(generally: display.readAndDispatch() never gets a =
chance to process and <BR>execute (or in SWT terms, read and dispatch!) =
the queued events. (because <BR>the programm just "STAYS" in your for =
loop, the whole time).<BR><BR>Here is a possible solution to the problem =
which I use often myself: I put <BR>the update loop in a seperate Thread, =
so that the UI-Thread can process the <BR>events without getting blocked. =
Note that display.syncExec() is used to pass <BR>the UI-changing instructio=
ns back to the UI-Thread...<BR><BR>For more info, take a look at the API =
docs for:<BR>org.eclipse.swt.widgets.Display<BR>&<BR >Display.syncExec(R=
unnable runnable)<BR>Display.asyncExec(Runnable runnable)<BR><BR>you might =
also want to take a look at the articles found on these pages, <BR>which =
both contain excellent tutorials for starters...:<BR><A href=3D"http://www.=
eclipse.org/articles">http://www.eclipse.org/articles</A><BR><A href=3D"htt=
p://www.cs.umanitoba.ca/~eclipse/">http://www.cs.umanitoba.ca/~eclipse/</A>=
<BR><BR>/* ***************** here is code********* */<BR><BR>import =
org.eclipse.swt.widgets.*;<BR>import org.eclipse.swt.*;<BR><BR>public =
class Temp implements Runnable<BR>{<BR> // display and =
text are made global so that the references are available <BR>within the =
run() method<BR> Display display;<BR> =
Text text;<BR><BR> // running is made global because =
otherwise it has to be declared final <BR>to be accessible<BR> &=
nbsp; // from the inner class<BR> boolean running;<BR><BR=
> public void run()<BR> {<BR> &nbs=
p; int y=3D1000000;<BR><BR> =
// if running is false, the shell is disposed and =
the thread should <BR>stop running<BR> &=
nbsp; // the variable running is used to prevent an Text.append(String =
<BR>str), after the text is<BR> =
// disposed (otherwise .append() would throw a <BR>WidgetDisposedException)=
<BR><BR> for (int x=3D0;x<y =
&& running;x++)<BR> =
{<BR> =
final int x2 =3D x;<BR><BR> =
// All calls to the GUI must be made within the =
UI-thread<BR> &n=
bsp; display.syncExec(new Runnable()<BR>  =
; {<BR> &n=
bsp; &n bsp; public void run()<BR>&=
nbsp; & nbsp; & nbs=
p; {<BR> &=
nbsp; & nbsp; if (!text.isDis=
posed())<BR> &nb=
sp; &nb sp; {<BR> =
&nb=
sp; &nb sp; // shell and text are still =
there: update<BR> &nbs=
p; &nbs p; &nbs p;&=
nbsp; text.append("\nCounter.. "+x2);<BR> &nbs=
p; &nbs p; &nbs p;&=
nbsp; System.out.print("\nCounter.. "+x2);<BR> =
;   ;   ;&n=
bsp; }<BR>  =
;   ;   ;&n=
bsp; else<BR> &n=
bsp; &n bsp; {<BR>  =
;   ;   ;&n=
bsp; &n bsp; // shell and text are =
disposed: stop running...<BR> &nbs=
p; &nbs p; &nbs p;&=
nbsp; running =3D false;<BR>  =
;   ;   ;&n=
bsp; }<BR>  =
; }<BR> &n=
bsp; });<BR> =
}<BR> }<BR><BR> public static void =
main(String[] args)<BR> {<BR> &nbs=
p; Temp t =3D new Temp();<BR> &nbs=
p; t.start();<BR> }<BR><BR> =
public void start()<BR> {<BR><BR> =
running =3D true;<BR>  =
; display =3D new Display();<BR> &=
nbsp; Shell shell =3D new Shell(display);<BR> =
text =3D new Text(shell,SWT.MULTI);<BR>  =
; text.setSize(200,200);<BR>  =
; text.setEditable(false);<BR> &nb=
sp; shell.open();<BR><BR> =
// shell is created, start the update thread...<BR> =
Thread thread =3D new Thread(this);<BR>  =
; thread.start();<BR><BR> &n=
bsp; // you dont need to open the shell twice...<BR>// &nb=
sp; shell.open();<BR> =
while (!shell.isDisposed())<BR> &=
nbsp; {<BR> &nbs=
p; if (!display.readAndDispatch())display.sleep();<BR> &nb=
sp; }<BR> =
display.dispose();<BR> }<BR>}<BR><BR><BR>regards,<BR><BR>=
Mani<BR><BR>"Matt McKenzie" <mmckenzie@novell.com> wrote in message =
<BR>news:cphqhm$nt5$1@<A href=3D"http://www.eclipse.org...">www.eclipse.org=
....</A><BR>Hi<BR><BR>I am new to java programing and even newer to SWT =
programming. I have found <BR>a problem where the GUI interface will =
"Hang". The background processes are <BR>all working but the GUI stops =
updating. (Running in a Windows XP <BR>environment)<BR><BR>I do not know =
if this is bug or a problem with my code. Below is a sample <BR>program =
that demonstrates the problem.<BR><BR>The program outputs a number to a =
text box and at the same time printing it <BR>to the system console. It is =
in a simple for loop. When the program is <BR>running press in quick =
succession the mouse about 10 to 20 times, The GUI <BR>will hang, however =
the System console is still being updated.<BR><BR>Is this a bug, or is =
their another way to do this.<BR><BR>I would be most gratefull for any =
assistance.<BR><BR>Thanks<BR><BR>Matt<BR><BR>Sample program<BR><BR>public =
class Temp {<BR><BR>public static void main(String[] args) {<BR> =
Temp t =3D new Temp();<BR> t.start();<BR>}<BR>public void start(){<BR=
> Display display =3D new Display();<BR> Shell shell =3D new =
Shell(display);<BR> Text text =3D new Text(shell,SWT.MULTI);<BR> =
; text.setSize(200,200);<BR> text.setEditable(false);<BR> =
shell.open();<BR><BR> int y=3D1000000;<BR> for (int x=3D0;x<=
y;x++){<BR> text.append("\nCounter.. "+x);<BR>  =
; System.out.print("\nCounter.. "+x);<BR> }<BR><BR> =
shell.open();<BR> while (!shell.isDisposed()){<BR> if =
(!display.readAndDispatch())display.sleep();<BR> }<BR> =
display.dispose();<BR>}<BR>} <BR><BR><BR><BR><BR></DIV></BODY></HTML>
--____WHPEPQYSAQXEHDGESJXG____--
--____LPHMXLZMXOMRLFKSEJCW____--
|
|
|
Goto Forum:
Current Time: Tue Sep 24 10:18:15 GMT 2024
Powered by FUDForum. Page generated in 0.04577 seconds
|