首页 文章

Delphi类参数

提问于
浏览
2

我试图在Delphi中编写自己的ThreadManager单元,到目前为止我有这个:

unit uThreadManager;

interface

uses
  Classes,
  Windows;

type
  TCustomTThread = class (TThread)
  public
    TaskData : Pointer;
end;

type
  TWorkerThread = class(TObject)
  private
    TaskDataList : TList;
    TaskDataListCrit : TRTLCriticalSection;
    function ReadTotalTasks : Integer;
  public
    constructor Create;
    destructor Destroy; override;
    property TotalTasks : Integer read ReadTotalTasks;
    function AddTask(Thread: TCustomTThread; Data: Pointer) : Integer;
    procedure Delete (Index : Integer);
end;

implementation

type
  PTaskData = ^TTaskData;
  TTaskData = record
  Thread          : TCustomTThread;
  TaskPointer     : Pointer;
end;

procedure TWorkerThread.Delete(Index: Integer);
var
  TaskData : PTaskData;
begin
  EnterCriticalSection(TaskDataListCrit);
  TaskData := TaskDataList.Items[Index];
  TaskDataList.Delete(Index);
  LeaveCriticalSection(TaskDataListCrit);
  TaskData^.Thread.Free;
  Dispose(TaskData);
end;

function TWorkerThread.ReadTotalTasks;
begin
  EnterCriticalSection(TaskDataListCrit);
  result := TaskDataList.Count;
  LeaveCriticalSection(TaskDataListCrit);
end;

destructor TWorkerThread.Destroy;
begin
  DeleteCriticalSection(TaskDataListCrit);
  TaskDataList.Free;
  inherited;
end;

constructor TWorkerThread.Create;
begin
  inherited;
  InitializeCriticalSection(TaskDataListCrit);
  TaskDataList    := TList.Create;
end;

function TWorkerThread.AddTask(Thread: TCustomTThread; Data: Pointer) : Integer;
var
  NewTask : PTaskData;
begin
  EnterCriticalSection(TaskDataListCrit);
  New(NewTask);
  // I would like to create a new instance of TCustomTThread here!
  //NewTask^.Thread       := ...
  NewTask^.TaskPointer  := Data;
  result                := TaskDataList.Add (NewTask);
  LeaveCriticalSection(TaskDataListCrit);
end;

end.

我在 AddTask 程序中遇到了参数问题...

这是我想要做的一个例子:

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, uThreadManager;

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

type
  TTheCustomThread = class (TCustomTThread)
  public
    procedure Execute; override;
end;

implementation

{$R *.dfm}

procedure TTheCustomThread.Execute;
begin
 // My Code
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  NewWorkerThread : TWorkerThread;
begin
  NewWorkerThread := TWorkerThread.Create;
  NewWorkerThread.AddTask(TTheCustomThread, NIL);
end;

end.

这段代码给了我错误:

[dcc32错误] Unit2.pas(42):E2010不兼容的类型:'TCustomTThread'和'TTheCustomThread类'

我可以通过在堆栈中声明一个新的 TTheCustomThread var来解决这个问题,但是我想避免这个原因我以后根本不需要它, AddTask 将创建一个 TTheCustomThread 的新实例 . 我可以使用TClass然后将类型转换为 TCustomThread ,但我想知道是否还有其他任何东西可以使这项工作 .

谢谢您的帮助 .

1 回答

  • 3

    您的函数 AddTask 定义如下:

    function AddTask(Thread: TCustomTThread; Data: Pointer) : Integer;
    

    您传递的第一个参数是 TCustomTThread 类型 . 这是 TCustomTThread 的一个实例 .

    你可以这样调用这个函数:

    AddTask(TTheCustomThread, nil);
    

    在这里传递类而不是实例 . 因此编译错误 .

    现在,您想要做的就是通过课程 . 在 AddTask 里面,您希望收到一个类,然后创建一个新实例 . 声明类类型如下:

    type
      TTheCustomThreadClass = class of TTheCustomThread;
    

    更改 AddTask 以接收该类型:

    function AddTask(ThreadClass: TCustomTThreadClass; Data: Pointer) : Integer;
    

    在实现中,创建一个这样的实例:

    NewTask^.Thread := ThreadClass.Create;
    

    您很可能希望将 TTheCustomThread 的构造函数声明为虚拟,以允许派生类自由定义可以从工厂创建机制执行的构造函数 .

相关问题