简体   繁体   中英

Eclipse RCP - DIsplay thread on Java

I have this function that should fire a notification window

public void PushNotif(String notifTitile,String notifText,int notifType) 
{
        Random r = new Random();
        int max = NotificationType.values().length;
        final String Title = notifTitile==null?"no Title":notifTitile;
        final String Text = notifText==null?"no Text":notifText;
        final int Type = notifType==0?r.nextInt(max):notifType;

        new Thread(new Runnable() 
        {
            public void run() 
            {
                while (true) 
               {
                  try 
                  {
                      Thread.sleep(1000); 
                  }
                  catch (Exception e) 
                  { 
                      e.printStackTrace(); 
                  }
                  Display.getDefault().syncExec(new Runnable() 
                  {
                     public void run() 
                     {
                          //IT'S NEVER ENTER HERE!!!
                          NotifierDialog.notify(Title, Text, NotificationType.values()[Type]);
                          while (!NotifierDialog._shell.isDisposed()) 
                          {
                              if (!Display.getDefault().readAndDispatch()) Display.getDefault().sleep();
                          }
                          Display.getDefault().dispose();
                     }
                     });
               }
            }
         }).start();
 }

My problem is that it never enters the run() function.

It's stuck on Display.getDefault().syncExec(new Runnable()... and that's all. I know Display.syncExec should wait until its can run but I don't know how to make it ready...

Help?

It looks to me that you are adding the Runnable to the wrong display thread. Use the current thread, not the default . Replace everywhere.

getCurrent()

Returns the display which the currently running thread is the user-interface thread for, or null if the currently running thread is not a user-interface thread for any display.

Display.getCurrent().syncExec(new Runnable() {
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM