主程序的,就写代码了。动态库在加载的时候,传入一个回调函数,动态库在创建新的线程的时候,利用这个回调函数将线程加到线程管理器中。 
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;type  TBaseThreadList = class(Tthread)
  private
    isTerminate : Boolean;
  public
    constructor create(b : Boolean);
    procedure setIsTerminate(b :Boolean);
  end;  TestA = class(TBaseThreadList)
  private
    procedure Execute;  override;
  public
  end;
  Testb = class(TBaseThreadList)
  private
    procedure Execute;  override;
  public
  end;
var
  Form1: TForm1;
  m_threadList : TList; //所有线程列表
procedure regThread(pThreadList : TBaseThreadList);
implementation{$R *.dfm}
//注册所有线程  可以作为回调使用
procedure regThread(pThreadList : TBaseThreadList);
begin
  m_threadList.Add(pThreadList);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  //创建线程
  TestA.create(false);
  Testb.create(false);end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//启动线程
end;
procedure TForm1.Button3Click(Sender: TObject);
var
  i : Integer;
begin
  //释放线程
  //让线程自己主动释放
  for i := 0 to m_threadList.Count - 1 do
  begin
    TBaseThreadList(m_threadList.Items[i]).setIsTerminate(true);
  end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
  i ,j: Integer;
begin
  //显示线程列表所有信息
  j := 0;
  for i := 0 to m_threadList.Count - 1 do
  begin
    if not TBaseThreadList(m_threadList.Items[i]).Terminated then
      inc(j);
  end;
  ShowMessage('线程总数:'+inttostr(m_threadList.Count)+'----没有结束的线程个数:'+inttostr(j));
end;
procedure TForm1.Button5Click(Sender: TObject);
var
  i : Integer;
begin
  //强制释放所有线程
  for i := 0 to m_threadList.Count - 1 do
  begin
    TBaseThreadList(m_threadList.Items[i]).Terminate;
  end;
end;
procedure TForm1.Button6Click(Sender: TObject);
var
  i : Integer;
  tpthread : TBaseThreadList;
begin
  //强制释放所有线程
  for i :=  m_threadList.Count - 1 downto 0 do
  begin
    if not TBaseThreadList(m_threadList.Items[i]).Terminated then
      TBaseThreadList(m_threadList.Items[i]).Terminate;
    tpthread := TBaseThreadList(m_threadList.Items[i]);
    m_threadList.Delete(i);
    tpthread := nil;
  end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
    m_threadList := tlist.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
  m_threadList.Clear;
  FreeAndNil(m_threadList);
end;
{ TparentThreadList }
constructor TBaseThreadList.create(b: Boolean);
begin
  inherited create(b);
  regThread(self);
end;
procedure TBaseThreadList.setIsTerminate(b: Boolean);
begin
  isTerminate := b;
end;
{ TestA }
procedure TestA.Execute;
begin
  inherited;
  while true do
  begin
    Sleep(10000);
    if self.isTerminate  then Self.Terminate;
  end;
end;
{ Testb }
procedure Testb.Execute;
begin
  while true do
  begin
    if self.isTerminate  then Self.Terminate;
    Sleep(1000);
  end;
end;
end.