⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 psithreadmgr.pas

📁 一个delphi的p2p控件的源代码
💻 PAS
字号:
unit PsiThreadMgr;

//******************************************************************************
// The original software is under
// Copyright (c) 1993 - 2000, Chad Z. Hower (Kudzu)
//   and the Indy Pit Crew - http://www.nevrona.com/Indy/
//
// Amended : November 2000, by Michael M. Michalak MACS for use with
// MorphTek.com Inc Peer to Peer Open Source Components - http://www.morphtek.com
//
//******************************************************************************

interface

uses
  Classes,
  PsiBaseComponent,
  PsiThread,
  SyncObjs;

type
  TPsiThreadMgr = class(TPsiBaseComponent)
  protected
    FActiveThreads: TList;
    FLock: TCriticalSection;
    FThreadClass: TPsiThreadClass;
  public
    constructor Create(AOwner: TComponent); override;
    function CreateNewThread: TPsiThread; virtual;
    destructor Destroy; override;
    function GetThread: TPsiThread; virtual; abstract;
    procedure ReleaseThread(AThread: TPsiThread); virtual; abstract;
    procedure TerminateThreads(Threads: TList); 
    property ActiveThreads: TList read FActiveThreads;
    property Lock: TCriticalSection read FLock;
    property ThreadClass: TPsiThreadClass read FThreadClass write FThreadClass;
  end;

implementation

uses
  PsiException,
  PsiGlobal,
  PsiResourceStrings,
  PsiTCPServer,
  SysUtils;

{ TPsiThreadMgr }

constructor TPsiThreadMgr.Create(AOwner: TComponent);
begin
  inherited;
  FActiveThreads := TList.Create;
  FLock := TCriticalSection.Create;
end;

function TPsiThreadMgr.CreateNewThread: TPsiThread;
begin
  if ThreadClass = nil then begin
    raise EPsiException.create(RSThreadClassNotSpecified);
  end;
  result := TPsiThreadClass(ThreadClass).Create;
end;

destructor TPsiThreadMgr.Destroy;
begin
  FreeAndNil(FActiveThreads);
  FreeAndNil(FLock);
  inherited;
end;

procedure TPsiThreadMgr.TerminateThreads(Threads: TList);
var
  Thread: TPsiThread;
  i: integer;
begin
  for i := Threads.Count - 1 downto 0 do begin
    Thread := TObject(Threads[i]) as TPsiThread;
    if Thread is TPsiPeerThread then begin
      TPsiPeerThread(Thread).Connection.Disconnect;
    end;
    ReleaseThread(Thread);
    Threads.Delete(i);
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -