Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How I can actualize label in a thread?
| | | |
wonderful!!!!!!!!!!!!! [message #446302 is a reply to message #446301] |
Tue, 23 November 2004 13:06 |
David Messages: 30 Registered: July 2009 |
Member |
|
|
Many many thanks!!!!!
This class example works perfectly.
¡¡Many thanks again!!
Now I've the base class to actualize all my graphics components.
Thank you very much.
Horst Dehmer wrote:
> Hi,
> if run() is the sync'd run() the problem might be the update of flag in this
> run() method. The following
> works for me. Check out the separation of label update and flag/step
> calculation in the BusyTimer
> class. Don't let populateShellHook() bother you. Just copy/use the
> BusyTimer.
> package org.eclipse.swt.samples;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.Shell;
> public class BusyThreadSample extends BasicSample {
> private Label label;
> protected void populateShellHook(Shell shell) {
> shell.setLayout(new FillLayout());
> Composite pane = new Composite(shell, SWT.NONE);
> label = new Label(pane, SWT.NONE);
> label.setSize(100, 20);
> label.setText("busy...");
> new BusyTimer().start();
> }
> class BusyTimer {
> Thread worker;
> boolean interrupted = false;
> int step = 0;
> String[] gimmicks = new String[] {"-", "/", "|", "\"};
> private void updateText() {
> step = (step + 1) % 4;
> Display.getDefault().syncExec(new Runnable() {
> public void run() {
> if(!label.isDisposed()) {
> label.setText("busy " + gimmicks[step]);
> }
> }
> });
> }
> public void start() {
> worker = new Thread() {
> public void run() {
> while(!interrupted) {
> updateText();
> try {
> Thread.sleep(250);
> }
> catch(InterruptedException dontCare) {
> interrupted = true;
> }
> }
> }
> };
> interrupted = false;
> worker.setDaemon(true);
> worker.start();
> }
> public void stop() {
> interrupted = true;
> worker.interrupt();
> worker = null;
> }
> }
> public static void main(String[] args) {
> new BusyThreadSample();
> }
> }
> "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
> news:cnv75j$g3l$1@www.eclipse.org...
>> Hi!
>>
>> Many thanks for your answer.
>>
>> Your code works fine but now I've other problem when
>> I iterate in the run method (the label is named messageLabel).
>> I wrote this lines:
>>
>> boolean continuar = true;
>>
>> public void run() {
>>
>> boolean flag = false;
>> while(continuar){
>> messageLabel.setText("" + (flag?"/":"""));
>> System.out.println(""+flag);
>> flag =!flag;
>>
>> try{
>> Thread.sleep(100);
>> }catch(Exception e){};
>> }
>> }
>>
>>
>>
>> During iterate I see the actualized flag variable value in the console but
>> the
>> dialog not actualices (Really it doesn't visible ¿?¿?).
>>
>> If I eliminate the iteration the dialog se OK and actualices the text
>> of the label.
>>
>> can you help me?
>>
>> Many thanks again!!
>>
>>
>> Horst Dehmer wrote:
>>
>> > Hi David!
>>
>> > As far as I know, calls to widgets must be made from within the swt ui
>> > thread.
>> > This might fix your problem:
>>
>> > Display.getDefault().syncExec(new Runnable() {
>> > public void run() {
>> > label.setText("some text...");
>> > }
>> > });
>>
>> > Hope this helps,
>> > Horst
>>
>> > "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
>> > news:cnv1c1$3q7$1@www.eclipse.org...
>> >> Hi!
>> >>
>> >> I'm using SWT and I've a problem.
>> >>
>> >> I need to connect to a database.
>> >> The time that the user must wait for the query is very long.
>> >> Because this, I open a Dialog with a label (named labelPorcentaje).
>> >> This label might change during the time that the database is processing
>> >> the petition.
>> >>
>> >> To do this, I created a thread that change the text.
>> >>
>> >> The problem is that with this thread the dialog not open or (in my
> tests)
>> >> it produces a thread error.
>> >>
>> >> I try lines as:
>> >>
>> >> dgpl.getShell().pack();
>> >> dgpl.getShell().redraw();
>> >>
>> >> but the problem persists.
>> >>
>> >>
>> >> The Dialog class code has this lines:
>> >>
>> >> labelPorcentaje.setLayoutData(gridData);
>> >> labelPorcentaje.setText("Iniciando proceso generación PL...");
>> >>
>> >> Hilo hilo = new Hilo();
>> >> hilo.run(labelPorcentaje);
>> >>
>> >>
>> >> And the Thread class (named Hilo) is here:
>> >>
>> >>
>> >> public class Hilo extends Thread{
>> >>
>> >> public void run(Label dgpl){
>> >>
>> >>
>> >> for (int y=0;y<99;y++){
>> >> System.out.println("Hello " + y);
>> >>
>> >> dgpl.setText("Number: "+y);
>> >>
>> >>
>> >> try {
>> >> sleep(100);
>> >> } catch (InterruptedException e) {
>> >> // TODO Bloque catch generado automáticamente
>> >> e.printStackTrace();
>> >> }
>> >> }
>> >>
>>
>>
|
|
| |
Hi again. Another problem... [message #446388 is a reply to message #446303] |
Wed, 24 November 2004 10:45 |
David Messages: 30 Registered: July 2009 |
Member |
|
|
Hi again.
I'm writting some code using your code examples and I've other problem.
I'm writting a code for Eclipse.
In this code, I create two threads using your code.
One of then shows the characters '-', '\', '-' ...
The other thread has a database access. When it finalices the connection
changes a class variable that is accesible with a method.
In the plugin code, I use a while sentence to wait that the second thread
ends:
while (myThread.connectionOpen()){
// Do nothing
}
//destroy thread 2 (connection)
//destroy thread 1 (windows with characters)
// Continues with the code plugin
The problems is that this iteration causes that visual components not
actulices.
(if I eliminate this iteration and not destroy the threads they execute
concurrently ok).
Where I can stop the principal code to wait for the second thread?
Many thanks!
Horst Dehmer wrote:
> de nada, Horst
> "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
> news:cnvcle$rbk$1@www.eclipse.org...
>> Many many thanks!!!!!
>>
>> This class example works perfectly.
>>
>> ¡¡Many thanks again!!
>>
>> Now I've the base class to actualize all my graphics components.
>>
>>
>> Thank you very much.
>>
>>
>>
>>
>>
>>
>>
>> Horst Dehmer wrote:
>>
>> > Hi,
>>
>> > if run() is the sync'd run() the problem might be the update of flag in
> this
>> > run() method. The following
>> > works for me. Check out the separation of label update and flag/step
>> > calculation in the BusyTimer
>> > class. Don't let populateShellHook() bother you. Just copy/use the
>> > BusyTimer.
>>
>> > package org.eclipse.swt.samples;
>>
>> > import org.eclipse.swt.SWT;
>> > import org.eclipse.swt.layout.FillLayout;
>> > import org.eclipse.swt.widgets.Composite;
>> > import org.eclipse.swt.widgets.Display;
>> > import org.eclipse.swt.widgets.Label;
>> > import org.eclipse.swt.widgets.Shell;
>>
>> > public class BusyThreadSample extends BasicSample {
>> > private Label label;
>>
>> > protected void populateShellHook(Shell shell) {
>> > shell.setLayout(new FillLayout());
>> > Composite pane = new Composite(shell, SWT.NONE);
>> > label = new Label(pane, SWT.NONE);
>> > label.setSize(100, 20);
>> > label.setText("busy...");
>> > new BusyTimer().start();
>> > }
>>
>> > class BusyTimer {
>> > Thread worker;
>> > boolean interrupted = false;
>> > int step = 0;
>> > String[] gimmicks = new String[] {"-", "/", "|", ""};
>>
>> > private void updateText() {
>> > step = (step + 1) % 4;
>> > Display.getDefault().syncExec(new Runnable() {
>> > public void run() {
>> > if(!label.isDisposed()) {
>> > label.setText("busy " + gimmicks[step]);
>> > }
>> > }
>> > });
>> > }
>>
>> > public void start() {
>> > worker = new Thread() {
>> > public void run() {
>> > while(!interrupted) {
>> > updateText();
>> > try {
>> > Thread.sleep(250);
>> > }
>> > catch(InterruptedException dontCare) {
>> > interrupted = true;
>> > }
>> > }
>> > }
>> > };
>>
>> > interrupted = false;
>> > worker.setDaemon(true);
>> > worker.start();
>> > }
>>
>> > public void stop() {
>> > interrupted = true;
>> > worker.interrupt();
>> > worker = null;
>> > }
>> > }
>>
>> > public static void main(String[] args) {
>> > new BusyThreadSample();
>> > }
>> > }
>>
>>
>>
>> > "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
>> > news:cnv75j$g3l$1@www.eclipse.org...
>> >> Hi!
>> >>
>> >> Many thanks for your answer.
>> >>
>> >> Your code works fine but now I've other problem when
>> >> I iterate in the run method (the label is named messageLabel).
>> >> I wrote this lines:
>> >>
>> >> boolean continuar = true;
>> >>
>> >> public void run() {
>> >>
>> >> boolean flag = false;
>> >> while(continuar){
>> >> messageLabel.setText("" + (flag?"/":"""));
>> >> System.out.println(""+flag);
>> >> flag =!flag;
>> >>
>> >> try{
>> >> Thread.sleep(100);
>> >> }catch(Exception e){};
>> >> }
>> >> }
>> >>
>> >>
>> >>
>> >> During iterate I see the actualized flag variable value in the console
> but
>> >> the
>> >> dialog not actualices (Really it doesn't visible ¿?¿?).
>> >>
>> >> If I eliminate the iteration the dialog se OK and actualices the text
>> >> of the label.
>> >>
>> >> can you help me?
>> >>
>> >> Many thanks again!!
>> >>
>> >>
>> >> Horst Dehmer wrote:
>> >>
>> >> > Hi David!
>> >>
>> >> > As far as I know, calls to widgets must be made from within the swt
> ui
>> >> > thread.
>> >> > This might fix your problem:
>> >>
>> >> > Display.getDefault().syncExec(new Runnable() {
>> >> > public void run() {
>> >> > label.setText("some text...");
>> >> > }
>> >> > });
>> >>
>> >> > Hope this helps,
>> >> > Horst
>> >>
>> >> > "David Díaz" <davidd@mapfre.com> schrieb im Newsbeitrag
>> >> > news:cnv1c1$3q7$1@www.eclipse.org...
>> >> >> Hi!
>> >> >>
>> >> >> I'm using SWT and I've a problem.
>> >> >>
>> >> >> I need to connect to a database.
>> >> >> The time that the user must wait for the query is very long.
>> >> >> Because this, I open a Dialog with a label (named labelPorcentaje).
>> >> >> This label might change during the time that the database is
> processing
>> >> >> the petition.
>> >> >>
>> >> >> To do this, I created a thread that change the text.
>> >> >>
>> >> >> The problem is that with this thread the dialog not open or (in my
>> > tests)
>> >> >> it produces a thread error.
>> >> >>
>> >> >> I try lines as:
>> >> >>
>> >> >> dgpl.getShell().pack();
>> >> >> dgpl.getShell().redraw();
>> >> >>
>> >> >> but the problem persists.
>> >> >>
>> >> >>
>> >> >> The Dialog class code has this lines:
>> >> >>
>> >> >> labelPorcentaje.setLayoutData(gridData);
>> >> >> labelPorcentaje.setText("Iniciando proceso generación PL...");
>> >> >>
>> >> >> Hilo hilo = new Hilo();
>> >> >> hilo.run(labelPorcentaje);
>> >> >>
>> >> >>
>> >> >> And the Thread class (named Hilo) is here:
>> >> >>
>> >> >>
>> >> >> public class Hilo extends Thread{
>> >> >>
>> >> >> public void run(Label dgpl){
>> >> >>
>> >> >>
>> >> >> for (int y=0;y<99;y++){
>> >> >> System.out.println("Hello " + y);
>> >> >>
>> >> >> dgpl.setText("Number: "+y);
>> >> >>
>> >> >>
>> >> >> try {
>> >> >> sleep(100);
>> >> >> } catch (InterruptedException e) {
>> >> >> // TODO Bloque catch generado automáticamente
>> >> >> e.printStackTrace();
>> >> >> }
>> >> >> }
>> >> >>
>> >>
>> >>
>>
>>
|
|
|
Re: Hi again. Another problem... [message #446390 is a reply to message #446388] |
Wed, 24 November 2004 11:27 |
Horst Dehmer Messages: 18 Registered: July 2009 |
Junior Member |
|
|
Hi David!
As far as I understand your goal you want to
show a busy indicator while some other thread
is accessing the db, right?
Before I would code this myself, I'd check
the Eclipse API's busy indicator for long lasting
operations (don't know how it's actually called).
But there are may examples in the Eclipse code
itself. I have to look at this in a couple of days,
too.
If you have to do it yourself, another thing is, that
you should gain deeper insight in coding with threads,
especially synchronizing concurrent execution. Have
a look at Thread.join(), Thread.wait(), Thread.notify()
and Thread.notifyAll() and above all the
synchronized(monitor) {} construct.
Concerning your code, the while-loop
while (myThread.connectionOpen()){
// Do nothing
}
will block the thread which executes it.
When I come up with something new, I'll let you
know.
Cheers,
Horst
|
|
|
Re: Hi again. Another problem... [message #446392 is a reply to message #446390] |
Wed, 24 November 2004 12:08 |
David Messages: 30 Registered: July 2009 |
Member |
|
|
Hi!
I'm trying to discover the method to do it too.
Ah! and many thanks for yout cooperation.
Horst Dehmer wrote:
> Hi David!
> As far as I understand your goal you want to
> show a busy indicator while some other thread
> is accessing the db, right?
> Before I would code this myself, I'd check
> the Eclipse API's busy indicator for long lasting
> operations (don't know how it's actually called).
> But there are may examples in the Eclipse code
> itself. I have to look at this in a couple of days,
> too.
> If you have to do it yourself, another thing is, that
> you should gain deeper insight in coding with threads,
> especially synchronizing concurrent execution. Have
> a look at Thread.join(), Thread.wait(), Thread.notify()
> and Thread.notifyAll() and above all the
> synchronized(monitor) {} construct.
> Concerning your code, the while-loop
> while (myThread.connectionOpen()){
> // Do nothing
> }
> will block the thread which executes it.
> When I come up with something new, I'll let you
> know.
> Cheers,
> Horst
|
|
|
Re: Hi again. Another problem... [message #446397 is a reply to message #446390] |
Wed, 24 November 2004 15:05 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
For an example of using the BusyIndicator for a long running task, see:
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet130.java?rev=HEAD& amp;content-type=text/vnd.viewcvs-markup
"Horst Dehmer" <horst.dehmer@onlinehome.de> wrote in message
news:co1r20$al3$1@www.eclipse.org...
> Hi David!
>
> As far as I understand your goal you want to
> show a busy indicator while some other thread
> is accessing the db, right?
>
> Before I would code this myself, I'd check
> the Eclipse API's busy indicator for long lasting
> operations (don't know how it's actually called).
> But there are may examples in the Eclipse code
> itself. I have to look at this in a couple of days,
> too.
>
> If you have to do it yourself, another thing is, that
> you should gain deeper insight in coding with threads,
> especially synchronizing concurrent execution. Have
> a look at Thread.join(), Thread.wait(), Thread.notify()
> and Thread.notifyAll() and above all the
> synchronized(monitor) {} construct.
>
> Concerning your code, the while-loop
>
> while (myThread.connectionOpen()){
> // Do nothing
> }
>
> will block the thread which executes it.
>
> When I come up with something new, I'll let you
> know.
>
> Cheers,
> Horst
|
|
|
Goto Forum:
Current Time: Tue Sep 24 00:47:49 GMT 2024
Powered by FUDForum. Page generated in 0.04118 seconds
|