WideStudio/MWT Logo
WideStudio/MWT
Programming Guide
WideStudio/MWT Index
Table of contents


Thread类



使用Thread类进行长时间的处理

利用WSCvthread 类的话,能运行需要长时间的处理。

运行长时间处理时,需将除Main Thread以外的Thread制作成通过THREAD-STARTED 触发器启动的事件过程。

#include <WScom.h>
#include <WSCfunctionList.h>
#include <WSCbase.h>
//----------------------------------------------------------
//Function for the event procedure
//----------------------------------------------------------
//作为Sub Thread运行的事件过程
void thread_ep(WSCbase* object){
  //长时间的处理
  //....
}

要运行Thread,须通过Thread Instance调用executeThread()成员函数。

//将THREAD-STARTED触发器所设定的事件过程
//作为Sub Thread进行实行
newvthr_000->executeThread();

由executeThread()成员函数所启动的THREAD-START 触发器事件过程内,由于Main Thread是作为异步Sub Thread动作,不能对Thread un-safe型的WideStudio 的 GUI Instance进行同步控制。 当需要通过WideStudio 的 GUI Instance来显示Thread的处理结果等情况时,使用 sendActivateEvent()成员函数,通过Thread Instance来通知处理结果。 调用sendActivateEvent()成员函数时,在Main Thread中,可启动ACTIVATE 活动,才能在事件过程中安全地对WideStudio 的 GUI Instance进行处理。
下面是 ACTIVATE 触发器中的Thread Instance例

#include <WScom.h>
#include <WSCfunctionList.h>
#include <WSCbase.h>
//----------------------------------------------------------
//Function for the event procedure
//----------------------------------------------------------

void thread_ep(WSCbase* object){
  //长时间处理
  //....

  //对Main thread通知ACTIVATE活动内容
  object->sendActivateEvent();
}

首先设定实行Main Thread的 ACTIVATE触发器的事件过程,可以如下实现与Sub Thread同步的WideStudio 的 GUI Instance表示处理等。

#include <WScom.h>
#include <WSCfunctionList.h>
#include <WSCbase.h>
//----------------------------------------------------------
//Function for the event procedure
//----------------------------------------------------------
//Main Thread开始动作
void activate_ep(WSCbase* object){
  //对Main Thread通知ACTIVATE的活动状态
  newvlab_000->setProperty(WSNlabelString,”Sub Thread的处理已完成!");
}



从Thread 访问WideStudio的GUI Instance

通过Thread类所生成的Sub Thread去访问Main Thread中运行的 WideStudio 的 GUI Instance时,首先锁定Main Thread,以防止竞争处理发生。

#include <WScom.h>
#include <WSCfunctionList.h>
#include <WSCbase.h>
//----------------------------------------------------------
//Function for the event procedure
//----------------------------------------------------------

void thread_ep(WSCbase* object){
  //在Sub Thread中的处理
  //....

  //一时锁定Main Thread,以防竞争发生
  WSGIappDev()->lock();
  //访问Main Thread上的WideStudio/MWT 的Instance 
  newvlab_000->setProperty(WSNlabelString,”Sub Thread的处理已完成!");
  WSGIappDev()->unlock();
}


Document Release 3.90 for WideStudio/MWT ver 3.90, Jul 2005


WideStudio/MWT documents index | Table of contents

Copyright(C) WideStudio/MWT Development Team, 1999-2005 Last modified: Jul 31, 2005