Name
BeginThread Function
Syntax
function BeginThread(SecurityAttributes: Pointer; StackSize: LongWord; ThreadFunc: TThreadFunc; Parameter: Pointer; CreationFlags: LongWord; var ThreadId: LongWord): Integer;
Description
Call BeginThread to
start a thread in a multithreaded program.
BeginThread calls the Windows API function
CreateThread, which starts a new thread and calls
the thread function (ThreadFunc) in the context of
the new thread. When the thread function returns, the thread
terminates. For more information about the security attributes or
creation flags, see the Windows API documentation for the
CreateThread function.
BeginThread returns the handle of the new thread
or zero if Windows cannot create the thread.
BeginThread is a real function.
Tips and Tricks
You should use
BeginThreadinstead of the Windows API functionCreateThreadbecauseBeginThreadsets the global variableIsMultiThreadto True.BeginThreadalso defines theThreadFuncandThreadIDparameters in Pascal style rather than C style.The thread function should catch and handle all exceptions. If the thread function raises an exception that it does not handle,
BeginThreadcatches the exception and terminates the application.Refer to Chapter 4, for more information about programming with threads.
Like any Windows resource, you must call
CloseHandleafter the thread terminates to make sure Windows releases all the resources associated with the thread. Delphi has a small memory leak if you start a thread in the suspended ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access