zoukankan      html  css  js  c++  java
  • Atitit.swt 线程调用ui控件的方法

    Atitit.swt 线程调用ui控件的方法

     

     

    1 SwingUtilities.invokeLater 1

    2 display.asyncExec方法 1

    3  display.timerExec(500,timer); 2

    4 、但有时候并不一定要程序执行时就要定时检测,有时需要外部事情激发这就出现了第2种解决方案,写一个内置类,可以放在事件监听的方法中,然后激发: 2

    5 参考 3

     

      SwingUtilities.invokeLater

     

      SwingUtilities.invokeLater(new Runnable(){                        @Override                        public void run() {                            label2.setText(x + "");                        }                    });

    这个问题我也碰到过,有个updateUI()方法,可以解决。

     

    display.asyncExec方法

    SWT提供的display.asyncExec方法,发现其实质根本不是另开一个线程,只是把run方法调用了一次,所以导致调用Thread.sleep时程序就会死掉。

    有时候是另外一个线程

     

     

    作者:: 老哇的爪子 Attilax 艾龙,  EMAIL:1466519819@qq.com

    转载请注明来源: http://blog.csdn.net/attilax

     

     display.timerExec(500,timer);

    1.  final Runnable timer = new Runnable () {  

    2.         int count = 0;  

    3.         public void run () {  

    4.           synchronized (this) {  

    5.             try {  

    6.               text.setText(  

    7.                 Integer.toString(count++));  

    8.             } catch (Exception e) {  

    9.               e.printStackTrace();  

    10.             }  

    11.           }  

    12.         }  

    13.       };  

    14.     while (shell != null && !shell.isDisposed()) {  

    15.         

    16.       if (!display.readAndDispatch())  

    17.         display.sleep();  

    18.       else   

    19.         display.timerExec(500,timer);  

     

     

    、但有时候并不一定要程序执行时就要定时检测,有时需要外部事情激发这就出现了第2种解决方案,写一个内置类,可以放在事件监听的方法中,然后激发:

    1.  new Thread() {  

    2.       private Runnable cmd = new Runnable() {  

    3.         public void run() {  

    4.           shell.setText(String.valueOf(counter++));  

    5.         }  

    6.       };  

    7.       public void run() {  

    8.         while (true) {  

    9.           try {  

    10.             Thread.sleep(2000);  

    11.           } catch (InterruptedException e) {  

    12.             return;  

    13.           }  

    14.           display.asyncExec(cmd);  

    15.         }  

    16.       }  

    17.     }  

    18.     .start();  

     

     

    参考

    SWT中定时器的一种特殊实现方式_SWT中线程互访时display.asyncExec_display.syncExec...程序死掉无响应的解决办法 - fm2005的专栏 博客频道 - CSDN.NET.html

    
  • 相关阅读:
    Luogu 4841 城市规划
    Luogu 4721 【模板】分治 FFT
    Luogu 4091 [HEOI2016/TJOI2016]求和
    Luogu 3723 [AH2017/HNOI2017]礼物
    FFT笔记
    Luogu 4900 食堂
    Luogu 4155 [SCOI2015]国旗计划
    Luogu 4069 [SDOI2016]游戏
    Luogu 4254 [JSOI2008]Blue Mary开公司
    Luogu 4251 [SCOI2015]小凸玩矩阵
  • 原文地址:https://www.cnblogs.com/attilax/p/15198840.html
Copyright © 2011-2022 走看看