![]() | Programming Guide | WideStudio/MWT Index Table of contents |
利用Thread类和WorkingDialog来实现长时间处理
利用Thread类和WorkingDialog来实现长时间处理
利用WSCvthread 类和 WSCworkingDialog 的话,可以实现需长时间的处理。
在这里将通过简单的例子来说明Thread类和WorkingDialog的实装方法。在此例中,将制作一个按下按钮时,需花长时间的处理将在Sub Thread上进行,表示WorkingDialog的应用软件。
首先,制作一个窗口,配置按钮,然后在各自上配置WSCworkingDialog 和 WSCvthread 类的Instance,并把名称作为newwork_001,newvthr_002 。
在按钮处理中,如下例一样设定事件过程btnop(),将花费时间的处理在Sub Thread上运行,表示WorkingDialog。在程序中,标志dialog_vis_status 是通过WorkingDialog的 popup方法向处理中的Sub Thread通知表示已经结束的部分。 当WorkingDialog被解除时,popup()方法经复归,如果设定标志为False,可定义在Sub Thread中WorkingDialog为非表示状态。#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <newwin000.h> //WorkingDialog是否表示的标志 WSCbool dialog_vis_status = False; void btnop(WSCbase* object){ //do something... dialog_vis_status = True; //在生成Thread之前定义标志 //生成Thread,开始实行处理。 newvthr_002->executeThread(); //设WorkingDialog为表示状态。 newwork_001->popup();//(注意)到WorkingDialog为非表示状态为止,不会复归。 dialog_vis_status = False; } static WSCfunctionRegister op("btnop",(void*)btnop);其次,对WSCvthread 类的Instance newvthr_002,通过THREAD-STARTED 触发器设定作为Sub Thread运行的处理事件过程。 为了可随时解除WorkingDialog,记述作为Sub Thread运行的事件过程的重要在于,将长时间的处理分成许多很短的处理,然后运行逐步处理。 对下列表现的例子,长处理被分成100 回进行,以变量 cnt 进行计数。并且,该计数也可用于表示WorkingDialog的进展情形。#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> #include <unistd.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <newwin000.h> extern long cnt; extern WSCbool dialog_vis_status; void thread_proc(WSCbase* object){ cnt=0; while(1){ //表示处理进展情形的计数。 cnt++; //为了将计数器的值反映在WorkingDialog上, //在Main Thread上运行 ACTIVATE 触发器的事件过程。 newvthr_002->sendActivateEvent(); if (dialog_vis_status == False){ //Dialog非表示,或被解除时结束处理。 return; } if (cnt > 100){ //如果运行了被分开的100回,就结束处理。 return; } //在这里将长时间的处理分成很短的处理逐步运行。 #ifdef MSW Sleep(1000); #else sleep(1); #endif } } static WSCfunctionRegister op("thread_proc",(void*)thread_proc);由于Instance为Thread un-safe,当在WorkingDialog上反映计数器值时,使用sendActivateEvent()方法,在Main Thread上启动ACTIVATE 的事件过程,由其来访问WorkingDialog。#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <newwin000.h> long cnt = 0; void act_ep(WSCbase* object){ //在属性中设定表示进展情形的计数值。 newwork_001->setProperty(WSNvalue,cnt); //如果计数器值为 100 以上, Dialog变为非表示, //结束处理。 if (cnt>100) { newwork_001->setVisible(False); } } static WSCfunctionRegister op("act_ep",(void*)act_ep);利用简易Thread类和WorkingDialog来实现长时间处理
以下是利用WSDthread 类和 WSCworkingDialog来运行长时间处理的例子。
首先准备一个窗口,配置按钮。然后配置WSCworkingDialog 类的Instance,名称作为 newwork_000。
在按钮处理中设定如下面一样的事件过程 btnop(),将花费时间的处理在Sub Thread上运行,,并表示WorkingDialog。
下面是通过使用存在标志 canceled ,利用WSDthread::terminateThread()方法灵活地强制解除Sub Thread的方法。 由于使用WSDthread::terminateThread()方法来停止Sub Thread时,是在处理途中强制停止,故不适合文件资源操作处理。
强制结束Sub Thread的例子。
#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> #include <WSDthread.h> #include <stdlib.h> #include <unistd.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <newwin000.h> WSDthread* thr = NULL; WSCbool canceled = False; //作为Sub Thread动作的过程 void* thread_func(WSDthread* obj,void*){ //计数器 long cnt= 0; while(1){ //表示处理进展情形的计数器。 cnt++; //将计数器的值反映在WorkingDialog上, //在Main Thread上运行过程。 obj->execCallback((void*)cnt); //在这里竟将长时间处理分成很短的处理,进行逐步处理。 #ifdef MSW Sleep(1000); #else sleep(1); #endif //如果计数器为100,结束处理。 if (cnt == 100){ return NULL; } } return NULL; } //将计数器值反映在WorkingDialog上的过程 //利用WSDthread::execCallback()方法在Main Thread上运行。 void callback_func(WSDthread*,void* val){ long cnt =(long)val; //将计数器值反映在WorkingDialog上 newwork_000->setProperty(WSNvalue,cnt); if (cnt == 100){ newwork_000->setVisible(False); } } //当按下按钮,表示WorkingDialog,开始处理。 void btnop(WSCbase* object){ if (thr == NULL){ //生成Thread Instance。 thr = WSDthread::getNewInstance(); //设定作为Sub Thread运行的过程 thr->setFunction(thread_func); //从Sub Thread来调用 //设定Main Thread上的Callback过程 thr->setCallbackFunction(callback_func); //生成Sub Thread thr->createThread(NULL); //表示WorkingDialog long val = newwork_000->popup(); if (val == WS_DIALOG_OK){ }else{ //强制结束Sub Thread。 thr->terminateThread(); } }else{ exit(0); } } static WSCfunctionRegister op("btnop",(void*)btnop);参照标志安全结束Sub Thread的例子。
#include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> #include <WSDthread.h> #include <stdlib.h> #include <unistd.h> //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- #include <newwin000.h> WSDthread* thr = NULL; WSCbool canceled = False; //作为Sub Thread动作的过程 void* thread_func(WSDthread* obj,void*){ //计数器 long cnt= 0; while(1){ //表示处理的进展情形的计数器。 cnt++; //为了让计数器值反映在WorkingDialog上, //在Main Thread上运行过程。 obj->execCallback((void*)cnt); //在这里将长时间处理分成很短的处理,逐步处理。 #ifdef MSW Sleep(1000); #else sleep(1); #endif //如果计数器成为了100,结束处理。 if (cnt == 100){ return NULL; } //参照标志,判断是否应该中断处理。 if (canceled != False){ return NULL; } } return NULL; } //为了将计数器值反映在WorkingDialog上的过程 // WSDthread::execCallback()方法在Main Thread上进行。 void callback_func(WSDthread*,void* val){ long cnt =(long)val; //将计数器反映在WorkingDialog上 newwork_000->setProperty(WSNvalue,cnt); if (cnt == 100){ newwork_000->setVisible(False); } } //当按下按钮,表示WorkingDialog,开始处理。 void btnop(WSCbase* object){ if (thr == NULL){ //生成Thread Instance。 thr = WSDthread::getNewInstance(); //作为Sub Thread设定运行过程 thr->setFunction(thread_func); //从Sub Thread来调用 //设定Main Thread上的Callback过程 thr->setCallbackFunction(callback_func); //生成Sub Thread thr->createThread(NULL); //表示WorkingDialog long val = newwork_000->popup(); if (val == WS_DIALOG_OK){ }else{ //中止Sub Thread处理。 canceled = True; } }else{ exit(0); } } static WSCfunctionRegister op("btnop",(void*)btnop);
Copyright(C) WideStudio/MWT Development Team, 1999-2005 | Last modified: Jul 31, 2005 |