📄 cthread.pas
字号:
unit Cthread;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
ThreadHandle: THandle; // holds the handles the the threads
ThreadHandle2: THandle;
CriticalSection: TRTLCriticalSection; // holds the critical section info
implementation
{$R *.DFM}
Function ThreadFunc(Info: Pointer): Integer; stdcall;
Var
Count : Integer; // general loop control variable
Begin
{performing the EnterCriticalSection function prevents the second thread
from executing until this thread leaves the critical section}
EnterCriticalSection(CriticalSection);
{show a visual display}
for Count := 0 to 100 Do
begin
Form1.Edit1.Text := IntToStr(Count);
Sleep(1);
end;
{display a message}
Form1.Edit1.Text := 'Hello from the thread!';
{pause for a second}
Sleep(1000);
{leave the critical section and exit the thread}
LeaveCriticalSection(CriticalSection);
ExitThread(4);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ThreadId1, ThreadId2: DWORD; // holds the created thread identifiers
begin
{initialize the critical section information}
InitializeCriticalSection(CriticalSection);
{create and execute the first thread}
ThreadHandle := CreateThread(nil, 0, @ThreadFunc, nil, 0, ThreadId1);
{create and execute the second thread}
ThreadHandle2 := CreateThread(nil, 0, @ThreadFunc, nil, 0, ThreadId2);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
{we are done, so destroy the critical section information}
DeleteCriticalSection(CriticalSection);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -