参考https://www.52pojie.cn/thread-802265-1-1.html 做下面的尝试
自己用delphi写的测试程序
下面是验证的一些图
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
gcsName: TRTLCriticalSection;
hThread: THandle;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
WaitForSingleObject(hThread,INFINITE);
end;
function MyThreadFun(p: Pointer): Integer; stdcall;
begin
EnterCriticalSection(gcsName);
LeaveCriticalSection(gcsName);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WaitForSingleObject(hThread,INFINITE);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
ID:DWORD;
begin
EnterCriticalSection(gcsName);
hThread:=CreateThread(nil, 0, @MyThreadFun, nil, 0, ID);
end;
initialization
InitializeCriticalSection(gcsName);
finalization
DeleteCriticalSection(gcsName);
end.
|