Delphi类参数

3

我正在尝试在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 Error] Unit2.pas(42): E2010 不兼容的类型: 'TCustomTThread' 和 'class of TTheCustomThread'

我可以通过在堆栈中声明一个新的 TTheCustomThread 变量来解决这个问题,但我想避免这样做,因为以后我将不需要它,并且 AddTask 将创建一个新的 TTheCustomThread 实例。我可以使用 TClass 然后强制转换为 TCustomThread,但我想知道是否还有其他方法使其工作。

感谢您的帮助。

1个回答

5

您的函数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的构造函数声明为虚函数,以允许派生类自由定义可以从您的工厂创建机制执行的构造函数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接