waitThread()

Waits for the end of the specified thread.

Synopsis

int waitThread(int threadId);

Parameters

Parameter Description
threadId ID number of the thread to be waited for.

Return value

0, in the event of errors -1.

Errors

Missing/incorrect argument, for example, the specified thread does not exist.

Description

Waits for the thread with the identification number threadId.

A thread can only be set to wait in the instance it was started.

It is only possible to wait for scripts, which have been started in the current script and it is not possible to wait for the current thread: waitThread(getThreadId()).

Example

void myFunction()
{
  DebugTN("begin of thread");
  int a;
  for (a = 0; a < 10; a++)
  {
    DebugTN(getThreadId(), a);
    delay(1);
    }
  DebugTN("end of thread");
}

/*
  A) with waitThread() -> main() is waiting after startThread()
  as long as the thread does not finish
*/

main()
{
  DebugTN("begin of main");
  int id = startThread("myFunction");
  waitThread(id);
  DebugTN("end of main");
}

/*
  Output in Log Viewer:
  WCCOAui1:2010.11.24 16:28:18.128["begin of main"]
  WCCOAui1:2010.11.24 16:28:18.191["begin of thread"]
  WCCOAui1:2010.11.24 16:28:18.191[1][0]
  WCCOAui1:2010.11.24 16:28:19.191[1][1]
  WCCOAui1:2010.11.24 16:28:20.191[1][2]
  WCCOAui1:2010.11.24 16:28:21.191[1][3]
  WCCOAui1:2010.11.24 16:28:22.191[1][4]
  WCCOAui1:2010.11.24 16:28:23.191[1][5]
  WCCOAui1:2010.11.24 16:28:24.191[1][6]
  WCCOAui1:2010.11.24 16:28:25.191[1][7]
  WCCOAui1:2010.11.24 16:28:26.191[1][8]
  WCCOAui1:2010.11.24 16:28:27.191[1][9]
  WCCOAui1:2010.11.24 16:28:28.191["end of thread"]
  WCCOAui1:2010.11.24 16:28:28.191["end of main"]
*/

/*
  B) without waitThread() -> main() will finish immediately after startThread() call
*/

main()
{
  DebugTN("begin of main");
  int id = startThread("myFunction");
//  waitThread(id);
  DebugTN("end of main");
}

/*
  Output in Log Viewer:
  WCCOAui1:2010.11.24 16:29:54.881["begin of main"]
  WCCOAui1:2010.11.24 16:29:54.959["begin of thread"]
  WCCOAui1:2010.11.24 16:29:54.959["end of main"]
  WCCOAui1:2010.11.24 16:29:54.959[1][0]
  WCCOAui1:2010.11.24 16:29:55.959[1][1]
  WCCOAui1:2010.11.24 16:29:56.959[1][2]
  WCCOAui1:2010.11.24 16:29:57.959[1][3]
  WCCOAui1:2010.11.24 16:29:58.959[1][4]
  WCCOAui1:2010.11.24 16:29:59.959[1][5]
  WCCOAui1:2010.11.24 16:30:00.959[1][6]
  WCCOAui1:2010.11.24 16:30:01.959[1][7]
  WCCOAui1:2010.11.24 16:30:02.959[1][8]
  WCCOAui1:2010.11.24 16:30:03.959[1][9]
  WCCOAui1:2010.11.24 16:30:04.959["end of thread"]
*/

Assignment

Threads

Availability

CTRL