Another multithreaded function in Delphi: BeginThread
When there are too many threads, there are too many threads, too many threads.
Delphi also provides similar functions with the same functions:
Function BeginThread (
SecurityAttributes: Pointer;
StackSize: LongWord;
ThreadFunc: TThreadFunc;
Parameter: Pointer;
CreationFlags: LongWord;
Var ThreadId: LongWord
): Integer;
When there are too many threads, there are too many threads, too many threads.
FHandle: = BeginThread (nil, 0, @ ThreadProc, Pointer (Self), create_suincluded, FThreadID );
Here we use the aforementioned Delphi RTL function BeginThread, which has many parameters. The key is the third and fourth parameters.
The third parameter is the previously mentioned thread function, that is, the Code executed in the thread.
The fourth parameter is the parameter passed to the thread function. Here it is the created thread object (Self ).
The fifth is used to set the thread to be suspended immediately after being created (the job of starting the thread is determined by the createsuincluded mark in AfterConstruction ),
The sixth is the return thread ID.
When there are too many threads, there are too many threads, too many threads.
Define the function called by BeginThread
When there are too many threads, there are too many threads, too many threads.
Declaration: type TThreadFunc: Function (Parameter: Pointer): Integer;
Description: TThreadFunc defines a function instead of data. This function is usually used as a parameter of the BeginThread function. BeginThread starts when a separate thread is running. This defined TThreadFunc function implements thread behavior.
The Code returned by the function is the exit code of the thread.
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} // contains the form definition
ThreadVar // each thread must be allowed to have an instance that is passed with the added record variable
MsgPtr: ^ TMsgRecord;
// Private thread process, used to display strings
Function ShowMsg (Parameter: Pointer): Integer;
Begin
// Set the return value to 0.
Result: = 0;
// Map the pointer to the passed Parameter
// Each thread has an independent msgPtr copy
MsgPtr: = Parameter;
// In the specified coordinate display dialog box
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
// Specify the thread function address
ShowMsgFunc: = Addr (ShowMsg );
// Initialize
Msg1.thread: = 1;
Msg1.msg: = 'Hello world ';
Msg2.thread: = 2;
Msg2.msg: = 'Goodbye world ';
// Start the first thread
Thread1: = BeginThread (nil,
0,
ShowMsgFunc,
Addr (msg1 ),
0,
Id1 );
// Start the second thread
Thread2: = BeginThread (nil,
0,
ShowMsgFunc,
Addr (msg2 ),
0,
Id2 );
// Ensure that the thread execution is complete before it is closed
ShowMessagePos ('Press this when other dialogs finished. ", 200,300 );
// Closes the thread.
CloseHandle (thread1 );
CloseHandle (thread2 );
End;
End.
The program running result is displayed in three dialog boxes:
Thread 1 Hello World
Thread 2 Goodbye World
Press this when other dialogs finished.