#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;

//running as the sub thread.
void* thread_func(WSDthread* obj,void*){
  long cnt= 0;
  while(1){
    cnt++;
    obj->execCallback((void*)cnt);
#ifdef MSW
    Sleep(1000);
#else
    sleep(1);
#endif
    if (cnt == 100){
      return NULL;
    }
    //terminate the thread softly
//    if (canceled != False){
//      return NULL;
//    }
  }
  return NULL;
}

//running on the main thread.
void callback_func(WSDthread*,void* val){
  long cnt = (long)val;
  //display the counter
  newwork_000->setProperty(WSNvalue,cnt);  
  if (cnt == 100){
    newwork_000->setVisible(False);
  }
}

//running on the main thread.
void btnop(WSCbase* object){
  //create a thread to count it up.
  if (thr == NULL){
    thr = WSDthread::getNewInstance();
    thr->setFunction(thread_func);
    thr->setCallbackFunction(callback_func);
    thr->createThread(NULL);
    long val = newwork_000->popup();
    if (val == WS_DIALOG_OK){
    }else{
      //terminate the thread compulsory
      thr->terminateThread();
      //terminate the thread softly
//      canceled = True;
    }
  }else{
    exit(0);
  }
}
static WSCfunctionRegister  op("btnop",(void*)btnop);
