SCADA实例解析五--脚本中实现多线程例子二

  这个例子是 SCADA实例解析三--脚本中实现多线程例子一  的改进版.  在这个例子中, 演示了在 SCADA页面中创建一个 线程对象($thread),

  并提供UI画面, 启动和停止这个线程.    

 

testThread2     

完整代码和注释如下:

  1 /*
  2     这是演示 SCADA脚本 中使用 线程对象(ThreadObject)的例子. 
  3     
  4 paul. 
  5 2012-11-1    
  6 */
  7 
  8 
  9 #begin design scada
 10 <LDL>
 11 
 12 %    $$I
 13 [type]    brick_combination
 14 
 15 %    $$I.sons
 16 []    $brick0
 17 []    $brick1
 18 []    $brick2
 19 
 20 
 21 %    $brick0
 22 [type]    brick_text
 23 (x)    6
 24 (y)    27
 25 (width)    567
 26 (height)    50
 27 [border.color]    Blue
 28 [border.type]    common
 29 [border.edge]    bump
 30 [border.edgeFlags]    RECT
 31 [bgn.type]    none
 32 (text)    text
 33 (color)    Black
 34 (font.name)    宋体
 35 (font.size)    18
 36 (font.weight)    400
 37 (alignX)    0
 38 (alignY)    0
 39 
 40 
 41 
 42 %    $brick1
 43 [type]    brick_text
 44 (x)    580
 45 (y)    26
 46 (width)    104
 47 (height)    50
 48 [border.color]    Blue
 49 [border.type]    common
 50 [bgn.type]    none
 51 (text)    启动线程
 52 (color)    Black
 53 (font.name)    宋体
 54 (font.size)    18
 55 (font.weight)    400
 56 (alignX)    0
 57 (alignY)    0
 58 
 59 
 60 
 61 %    $brick2
 62 [type]    brick_text
 63 (x)    691
 64 (y)    25
 65 (width)    104
 66 (height)    50
 67 [border.color]    Blue
 68 [border.type]    common
 69 [bgn.type]    none
 70 (text)    停止线程
 71 (color)    Black
 72 (font.name)    宋体
 73 (font.size)    18
 74 (font.weight)    400
 75 (alignX)    0
 76 (alignY)    0
 77 
 78 
 79 
 80 </LDL>
 81 #end design scada
 82 
 83 // 记数变量.
 84 $count = 0 ;
 85 
 86 // 设置1000ms的timer, 更新UI上 显示信息. 
 87 $$scadaWnd->setTimeout( 1000, "testTimer();" ) ;
 88 
 89 // $$scadaWnd代表SCADA窗体,  创建一个$$scadaWnd管理的线程对象($thread) 
 90 $thread = $$scadaWnd->newThreadObj() ; 
 91 
 92 // 绑定积木( $brick1 )的 onClick事件:  启动线程( 执行 testThread()方法  ) 
 93 $brick1.onClick = "$thread->asynRunScript( 'testThread() ;'  );" ;
 94 
 95 // 绑定积木( $brick2 )的 onClick事件:  停止线程. 
 96 $brick2.onClick = "$thread->endThread();" ;
 97 
 98 // 方法:  记数变量($count)++,  并且刷新UI.  
 99 function testTimer()
100 {
101 // 注意: 外部变量在方法中, 使用前需要先申明为外部变量, 否则是 内部变量.
102 //  这$count是 外部变量, 因此先申明为 外部变量.  
103 extern $count ;
104     $count++ ;
105     trace $count ;     // 调试输出. 
106 
107 // 刷新UI上 $count显示信息( 积木($brick0)显示 $count信息 ), 并让$$scadaWnd重画.      
108 extern $brick0    ;
109 extern $$scadaWnd ;
110     $$scadaWnd->setTimeout( 1000, "testTimer();" ) ;
111     $brick0->setText( $count ) ;
112     $$scadaWnd->redrawWnd() ;
113 
114 }
115 
116 // 测试线程. 
117 function testThread() 
118 {
119 extern $count ;
120 extern $thread ;
121 
122     // $thread的 控制状态:  
123         // 运行:          1
124         // 要求结束线程:     0        (  endThread()动作会设置 ThreadStatus为0 ). 
125         // 线程退出:        -1    
126     while( $thread->getThreadStatus()>0 )
127     {
128         $count++ ;
129         sleep(100) ;
130     }
131     $thread->setTheadStatus(-1) ;
132     
133 }

 

 

posted on 2012-11-01 14:36  smartfish_liu  阅读(1643)  评论(0编辑  收藏  举报

导航