DelphiBasics
TThreadFunc
Type
Defines the function to be called by BeginThread System unit
  type TThreadFunc : Function(Parameter : Pointer) : Integer;
Description
The TThreadFunc type defines a function rather than data. This function is normally used as a parameter to a BeginThread function call when starting a separate thread of operation. The defined function performs the thread activities.
 
The return code for the function is the exit code for the thread.
Related commands
BeginThread Begins a separate thread of code execution
EndThread Terminates a thread with an exit code
ThreadVar Defines variables that are given separate instances per thread
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : A simple example
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;

interface

uses
  Forms, Dialogs, Windows, SysUtils;

type
  TMsgRecord = record
    thread : Integer;
    msg    : string[30];
  end;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

Implementation
{$R *.dfm}        // Include form definitions

ThreadVar         // We must allow each thread its own instances
                  // of the passed record variable
  msgPtr : ^TMsgRecord;

// Private thread procedure to show a string
function ShowMsg(Parameter : Pointer) : Integer;
begin
  // Set up a 0 return value
  Result := 0;

  // Map the pointer to the passed data
  // Note that each thread has a separate copy of msgPtr
  msgPtr := Parameter;

  // Display this message
  ShowMessagePos('Thread '+IntToStr(msgPtr.thread)+' '+msgPtr.msg,
                 200*msgPtr.thread, 100);

  // End the thread
  EndThread(0);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  id1, id2 : LongWord;
  thread1, thread2 : Integer;
  msg1, msg2 : TMsgRecord;
  showMsgFunc : TThreadFunc;

begin
  // Set up the thread function
  showMsgFunc := Addr(ShowMsg);

  // Set up our display messages
  msg1.thread := 1;
  msg1.msg    := 'Hello World';
  msg2.thread := 2;
  msg2.msg    := 'Goodbye World';

  // Start the first thread running asking for users first name
  thread1 := BeginThread(nil,
                         0,
                         showMsgFunc,
                         Addr(msg1),
                         0,
                         id1);

  // And also ask for the surname
  thread2 := BeginThread(nil,
                         0,
                         showMsgFunc,
                         Addr(msg2),
                         0,
                         id2);

  // Ensure that the threads are only closed when all done
  ShowMessagePos('Press this when other dialogs finished.', 200, 300);

  // Finally, tidy up by closing the threads
  CloseHandle(thread1);
  CloseHandle(thread2);
end;

end.
   Three dialogs are displayed:
     
   Thread 1 Hello World
   Thread 2 Goodbye World
   Press this when other dialogs finished.
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page