CreateThread Procedure

Creates a thread.

Syntax

[ hThread = ] CreateThread ( proc [, lParameter] [,bSuspended] [,lId] )

The CreateThread procedure syntax has the following parts:

 

Name

Type

Description

lHandle

AHandle

Handle to the thread.

proc

Val Procedure

Thread procedure. See Comments.

lParameter

Val Long

Specifies a single 32-bit parameter value passed to the thread.

bSuspended

Val Bool

A Boolean expression specifying whether the thread is to be suspended after it is created.

hThreaad

Var Long

Handle for the thread. 0 if error occurred and the thread creation failed.

Where

bSuspended can have one of the following values:

Value

Description

True

The thread is to be suspended.

False *

The thread is running.

Comments

The CreateThread function creates a thread to run in parallel to the caller thread, all run in the same process sharing the same address space. The procedure passed get execute in parallel to the caller thread.

The caller can use for WaitForSingleObject() internal function to wait until the thread exit.

The procedure (proc) must have the following prototype:

ThreadProcedure(lParameter : Val Long): Void

The called thread will end when the ThreadProcedure returns (using return statement or reached to the end of the procedure), or by calling ExitThread() internal function.

Example

The following example creates a thread that reads data:

hThread=CreateThread(ReadThreadProc, 10)
if hThread=0 then
   TestStatus=FAIL
   exittest
endif

! wait until the thread is over or 2000 ms
if WaitForSingleObject(hThread, 2000)<>aWaitObject0 ! should be done by now
   TestStatus=FAIL
endif

The following shows an example for the thread procedure (ReadThreadProc):

 

Procedure ReadThreadProc (lBytes): Void
------------------------------------------------------------------------

lBytes: Val Long

{

   while ReadAppendByte(m_sBuffer)>0

    continue

    if len(m_sBuffer)>lBytes

       return   ! exit the thread

    endif

endwhile

! no more data exit the thread

}

 

 

Procedure ReadAppendByte (): Long

!------------------------------------------------------------------------

{

! read lBytesRead

if ChecckStatus()<0

   ExitThread()   ! end the thread

endif

return ReadAppendByte(m_sBuffer)

}

 

See Also

ExitThread, SuspendThread, TerminateThread, SetThreadPriority, WaitForSingleObject, WaitForMultipleObjects