第一种最简单即是使用delphi封装的synchronize(procedure)这个函数。
这个函数他把procedure中的代码,放到主线程中执行。所以是串行化,每次执行一个
线程。
procedure TMyThread.MyProcedure;
begin
 //你的代码
end;
procedure TMyThread.Execute;
begin
  Synchronize(MyProcedure);
end;
然后是有TCriticalSection这个类。他有两个方法:Enter,Leave
也是每次只执行一个线程。
var CS:TCriticalSection;
procedure TForm.OnCreate();
begin
          CS:=TCriticalSection.Create;
end;
procedure TMyThread.Execute;
begin
         CS.Enter;
         //你的代码
         CS.Leave;
end;
其他就是windows api函数,创建内核对象。
如CreateMutex,CreateEvent,CreateSemaphore
创建后要用waitforsingleobject或waitformultipleobject.
但这些函数复杂些但功能也更强大。可以同时运行多个线程。
但代码如下,都差不多。
procedure TForm.OnCreate()
begin
          CreateMutex(nil,True,'mymutex');
end;
procedure TMyThread.Execute;
var hand:THandle;
begin
          hand:=OpenMutex(MUTEX_ALL_ACCESS,False,'mymutex');
          WaitForSingleObject(hand,INFINITE);
          //你的代码
end;