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


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

     

     

    1 SwingUtilities.invokeLater1

    2 display.asyncExec方法1

    3  display.timerExec(500,timer);2

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

    5 参考3

     

    1   SwingUtilities.invokeLater

     

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

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

     

    2 display.asyncExec方法

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

    有时候是另外一个线程

     

     

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

    转载请注明来源: http://www.cnblogs.com/attilax/

     

    3  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);  

     

     

    4 、但有时候并不一定要程序执行时就要定时检测,有时需要外部事情激发这就出现了第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();  

     

     

    5 参考

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

  • 相关阅读:
    《软件测试自动化之道》读书笔记 之 底层的Web UI 测试
    《软件测试自动化之道》读书笔记 之 基于Windows的UI测试
    《软件测试自动化之道》读书笔记 之 基于反射的UI测试
    MS UI Automation Introduction
    Server-Side UI Automation Provider
    Client-Side UI Automation Provider
    Server-Side UI Automation Provider
    河边的苹果
    CommonJS和AMD/CMD
    webpack
  • 原文地址:https://www.cnblogs.com/attilax/p/4782601.html
Copyright © 2020-2023  润新知