Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT Threads
SWT Threads [message #630986] Tue, 05 October 2010 18:39 Go to next message
larsk is currently offline larsk
Messages: 22
Registered: October 2009
Location: Sweden
Junior Member
I am creating an application in Java with SWT where I have a main shell from where I can open several other shells. Each new shell is created in a new thread and opened from a button on the main shell.
But when one of the other shells is working, the other shells is unresponsive.
The code is something like the following code but when I press the button, a new shell is created and opened. I just created this to show that the list isn't updating when clicking in it while the other thread is working.
Is there a way to make the other shells responsive while one shell is working?


package threadtest;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

public class Main extends Shell
{
    List list = new List (this, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
    Button buttonStart = new Button(this, SWT.NONE);

    public static void main(String[] args)
    {
        Main main = new Main();
    }

    Main()
    {
        super(Display.getDefault(), SWT.CLOSE | SWT.TITLE | SWT.MIN);
        this.setText("ThreadTest");
        this.setBounds(150, 150, 200, 340);
        
        buttonStart.setText("Start");
        buttonStart.setLocation(100, 270);
        buttonStart.pack();
        buttonStart.addSelectionListener(startListener);

        list.setBounds(10, 10, 150, 250);

        for (int index = 0; index < 10; index++)
            list.add("Item " + index);

        this.open();

        while (!this.isDisposed())
        {
            if (!Display.getDefault().readAndDispatch())
                Display.getDefault().sleep();
        }
    }

    @Override
    protected void checkSubclass()
    {
        // Disable the check that prevents subclassing of SWT components
    }

    SelectionListener startListener = new SelectionListener()
    {
        @Override
        public void widgetSelected(SelectionEvent event)
        {
            Thread threadStart;
            threadStart = new Thread(startRunnable);
            threadStart.start();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent event)
        {

        }
    };

    Runnable startRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            Display.getDefault().asyncExec (new Runnable ()
            {
                @Override
                public void run ()
                {
                    for (int index = 0; index < 100000; index++)
                        System.out.println("index: " + index);
                    
                    System.out.println("Done");
                }
            });
        }
    };
}
Re: SWT Threads [message #631006 is a reply to message #630986] Tue, 05 October 2010 19:54 Go to previous messageGo to next message
David Wegener is currently offline David Wegener
Messages: 1341
Registered: July 2009
Senior Member
SWT is single threaded. All access to widgets have to be on the Display thread.

You are creating threads, but then having them execute a runnable on the Display thread. While the runnable is in the long for loop, the Display thread can't do anything else.

You seem to have the concept backwards. You don't want to pass a long running process to the Display thread. You want to perform the long running process on a separate thread. You should only call Display.asyncExec() when you need to update/access a widget.
Re: SWT Threads [message #631007 is a reply to message #631006] Tue, 05 October 2010 20:00 Go to previous messageGo to next message
larsk is currently offline larsk
Messages: 22
Registered: October 2009
Location: Sweden
Junior Member
Thanks for your answer!

The above code was just an example. In the real code I am updating/accessing some SWT components. I just didn't want to post the whole code.
How should I do if I want to update a shell and still be able to use another shell?
Re: SWT Threads [message #631049 is a reply to message #631007] Tue, 05 October 2010 23:53 Go to previous message
David Wegener is currently offline David Wegener
Messages: 1341
Registered: July 2009
Senior Member
On Tue, 2010-10-05 at 22:00 +0200, larsk wrote:
> Thanks for your answer!
>
> The above code was just an example. In the real code I am updating/accessing some SWT components. I just didn't want to post the whole code.
> How should I do if I want to update a shell and still be able to use another shell?

Simply create the second shell. The SWT Display thread will handle both
shells.
Previous Topic:context menu appears on mouseUp under Windows, but on mouseDown under Mac OS
Next Topic:SWT Applet, webstart and Invalid thread access
Goto Forum:
  


Current Time: Thu Oct 02 14:45:18 GMT 2014

Powered by FUDForum. Page generated in 0.01712 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software