Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] Working with different threads in SWT

Hi,

you should have posted this kind of question to the the swt newsgroup (http://www.eclipse.org/newsgroups/). This mailing list is not for user questions but for internal communication of developers. I'll answer your question now here but please use the newsgroup for this kind of question.

Your problem is that you are executing the threads in the Runnable which is simply wrong. Normally your code looks like the following:

public class Rotate {

   public void static rotate( final Image image, final Composite comp) {
       final Display d = comp.getDisplay();

       Thread t = new Thread() {
           public void run() {
	       for (int i = 0 ; i <= rect.width / 2 ; i++) {
                   d.syncExec(new Runnable() {
                       public void run() {
                           comp.setBounds(
                               rect.x + i,
                               rect.y ,
                               rect.width - i*2,
                               rect.height)
                       }
                   });
                   Thread.sleep(1);
               }

               // ...
           }
       };

       t.start();
   }
}

The key point is that you only sync the calls you make to SWT-Components because the Disply#syncExec takes care that those parts of code are executed in the SWT-Event Thread. You never ever start new threads inside a runnable you pass to syncExec/asyncExec.

Tom

Roman Kournjaev schrieb:
Hi Tom

I am sorry for bothering you with my questions , but i simply don't understand where do i post the code , i got to this link http://dev.eclipse.org/newslists/news.eclipse.platform.swt/maillist.html , i searched a little through the site but there is not such options as post , or open theme. Regarding the question the thread spawning is done in the main thread , here is the runnable class , which is launched asynchrony.


import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;

class Rotate implements Runnable
{
    private Image image;
    private Composite comp;

    public Rotate(Image image, Composite comp) {
        this.image = image;
        this.comp = comp;
    }
    public void run()
    {
        Rectangle rect = comp.getBounds();
        try{
            for (int i = 0 ; i <= rect.width / 2 ; i++)
            {
comp.setBounds(rect.x + i, rect.y , rect.width - i*2 , rect.height);
                Thread.sleep(1);
}
            for (int i = rect.width / 2 - 1 ; i >= 0  ; i--)
            {
comp.setBounds(rect.x + i , rect.y , rect.width - i*2 , rect.height);
                Thread.sleep (1);
            }
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        comp.setBackgroundImage(image);
    }

}

Sie konnen mir auch auf Deutsch antworten.
Vielen Dank im Vorraus!

Roman

On 3/25/07, *Tom Schindl* <tom.schindl@xxxxxxxxxxxxxxx <mailto:tom.schindl@xxxxxxxxxxxxxxx>> wrote:

    Hi,

    Please post the code you are running and the question to the
    platform.swt newsgroup. I suppose you are doing all the thread spawing,
    ... in the runnable which is simply wrong. You should the only lines of
    code you should run in the Runnable are the calls to the SWT-Functions.

    Tom

    Roman Kournjaev schrieb:
     > Hello
     >
     > I have some synchronization problem , I would very appreciate it
    if one
     > of you could help me out.
     >
     > I have a class that implements Runnable interface , this class is
    doing
     > some manipulations(for instance setBounds) on a Comosite object
    from SWT
     > , that is connected to some Shell. I am starting two such Threads
    with
     > the Display.getDefault().asyncExec(runnalbe) function (every Threads
     > gets a different Composite object) , but The threads are not
    executed in
     > an asynchrony way ,  the first thread is waiting for the other to die
     > and only then it starts its actual execution. (sleeping and yielding
     > inside the thread didn't help).
     >
     > Thanks in advance for your help
     >
     > Roman
     >
     >
     >
    ------------------------------------------------------------------------
     >
     > _______________________________________________
     > platform-swt-dev mailing list
     > platform-swt-dev@xxxxxxxxxxx <mailto:platform-swt-dev@xxxxxxxxxxx>
     > https://dev.eclipse.org/mailman/listinfo/platform-swt-dev

    _______________________________________________
    platform-swt-dev mailing list
    platform-swt-dev@xxxxxxxxxxx <mailto:platform-swt-dev@xxxxxxxxxxx>
    https://dev.eclipse.org/mailman/listinfo/platform-swt-dev



------------------------------------------------------------------------

_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/platform-swt-dev



Back to the top