Results 1 to 3 of 3
  1. #1
    prueba2306 is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default Creating a thread that modifies UI

    Hello:

    I need some help about where I can find information about how to create a Thread that will fill information in a SWT List. I created a view, and inside this view I have a List control. When the plugin is loaded I need that a Thread must be created and this Thread will check for a file that another program will update, when the file is updated, the thread adds the new information into the List control.

    I been searching how to do that and the only information that I found is about the Display object and asynExec/syncExec methods. Here is an example of the code that I wrote:

    Java Code:
    public class MyView extends ViewPart {
       
       List lst;
       Display dsp;
       Runnable run;
       
       @Override
       public void createPartControl(Composite parent) {
          // TODO Auto-generated method stub
          this.lst = new List(parent, SWT.SINGLE);
          this.lst.setBounds(50, 50, 75, 75);
          
          run = new Runnable() {
             public void run() {
                // Infinite loop code that reads the file and update the List control...
             }
          };
          dsp = Display.getDefault();
          dsp.asyncExec(run);
       }
    
       @Override
       public void setFocus() {
          // TODO Auto-generated method stub
          
       }
    
    }
    The problem with this code is that when the Plugin is loaded, Eclipse doesn't respond. I think its because the thread is not running asynchronously but I havent found information of how to do this on the web.

    I will appreciate any reference of how to do that. Thanks for your help and time.

    See ya.

  2. #2
    prueba2306 is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default

    Well... I found a possible solution but being honest, I don't like it. You can see this solution on this page: eclipse.dzone.com/news/system-tray-icon-monitoring-hudson-eclipse-rcp

    I don't like it because you are rescheduling the same thread over and over and over... and I think that this will give problems if I have to manage synchronization and the issue about sleep the thread so the GUI can gain control.

    This is the solution that I perform based on the previous page:

    Java Code:
    package com.example.myplugin;
    
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.ui.part.ViewPart;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.*;
    
    public class MyView extends ViewPart {
       
       List lst;
       
       @Override
       public void createPartControl(Composite parent) {
          // TODO Auto-generated method stub
          this.lst = new List(parent, SWT.SINGLE);
          this.lst.setBounds(50, 50, 75, 75);
          lst.getDisplay().asyncExec(new Runnable() {
             public void run() {
                if(!lst.isDisposed()) {
                   // Read file, update lst if there is something new
                   lst.getDisplay().asyncExec(this);
                }
             }
          });
       }
    
       @Override
       public void setFocus() {
          // TODO Auto-generated method stub
       }
    
    }
    I recommend verify previously if lst is disposed "(!lst.isDisposed()) {...}" instead verify the display because the lst object is disposed first than the display and you may get an exception. Please, I will appreciate a better solution...

  3. #3
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    If you were using Swing I would tell you to use SwingWorker, the source code is available and you could try modify it for SWT.

    Try modify it and change all references to SwingUtilities.invokeLater or EventQueue.invokeLater to Display.asyncExec.

    The only two methods you would have to override would be doInBackground() and process(), and you would have to pass the Display object to the thread at construction.

    Your infinate loop would be in the doInBackground and any updates that effect the GUI would be submitted via the publish method and handled in the process method.

    I am not at all familiar with SWT so i could be talking utter rubbish and this will not work. If you do try it please post the results.

    HTH
    Stephen

Similar Threads

  1. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 11:30 PM
  2. Creating a Thread using the Runnable interface
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:31 PM
  3. Replies: 0
    Last Post: 01-28-2008, 08:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •