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 BeginThread instead of the Windows API function CreateThread because BeginThread sets the global variable IsMultiThread to True. BeginThread also defines the ThreadFunc and ThreadID parameters 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, BeginThread catches the exception and terminates the application.

  • Refer to Chapter 4, for more information about programming with threads.

  • Like any Windows resource, you must call CloseHandle after 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 ...

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.