哎呀,给个例子给你!列出的是部分代码,包含关键部分,下面的与完整的差不了多少了!
type
TMainForm=class(TForm)
Button1:TButton;
ListBox1:TListBox;
procedure Button1Click(Sender:TObject);
private
procedure ThreadDone(Sender:TObject);
end;
TMyThread=class(TThread)
protected
procedure Execute;override;
end;
var
MainForm:TMainForm;
DoneNum:integer;
hMutex:THandle=0;
//线程体
procedure TMyThread.Execute;
var
i:integer;
begin
FreeOnTerminate:=true;
OnTerminate:=MainForm.ThreadDone;
if WaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0 then
begin
for i:=1 to 100do
Sleep(5);
end;
//释放互斥
ReleaseMutex(hMutex);
end;
procedure TMainForm.ThreadDone(Sender:TObject);
var
i:integer;
begin
Inc(DoneNum);
ifdo
neNum=2 then
begin
for i:=1 to 100do
ListBox1.Items.Add(IntToStr(i));
//关闭互斥句柄
CloseHandle(hMutex);
end;
end;
procedure TMainForm.Button1Click(Sender:TObject);
begin
//创建互斥
hMutex:=CreateMutex(nil,false,nil);
TMyThread.Create(false);
//创建线程
TMyThread.Create(false);
end;