简体   繁体   English

Delphi - 未创建自定义线程

[英]Delphi - Custom thread not being created

i'm having a bit of a problem here. 我在这里遇到了一些问题。 I have a custom class that inherits TPersistent class, inside(private section) this custom class, i have a custom made TThread with overriden Execute method which fires every (1000 ms). 我有一个继承TPersistent类的自定义类,在这个自定义类的内部(私有部分),我有一个定制的TThread,带有覆盖Execute方法,每次触发1000毫秒。 Everything works great, until i move my 2 custom classes to a new Unit... 一切都很好,直到我将我的2个自定义类移动到新单元...

type
  TMyThread= class(TThread)
  protected
   procedure Execute; override;
  end;

  TMyClass = class(TPersistent)
  private
   T: TMyThread;
  protected
   constructor Create;
  public
   destructor Destroy; override;
  end;


implementation

procedure TMyThread.Execute;
begin
 while not Self.Terminated do begin
  Sleep(1000);
  MessageBox(0, 'test', nil, MB_OK)
 end;
end;

constructor TMyClass.Create;
begin
 inherited Create;
 t := TMyThread.Create(False);
end;

destructor TMyClass.Destroy;
begin
 t.Terminate;
 t.WaitFor;
 FreeAndNil(t);
 inherited Destroy;
end;

The above code works great in the main project unit, but when i move it to a new unit, the thread code no longer works, i get an AV, when i try to free a TMyClass object. 上面的代码在主项目单元中工作得很好,但当我将它移动到一个新的单元时,线程代码不再有效,我得到一个AV,当我尝试释放一个TMyClass对象时。 I think the thread is not being constructed at all, and that's why i get an AV when i try to free it... but why? 我认为线程根本没有被构造,这就是为什么当我试图释放它时我得到AV ...但为什么呢? it shouldn't matter in which Unit the code is... 代码是哪个单元应该无关紧要......

Unit1: 单元1:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, unit2;

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

var
  Form1: TForm1;
  p: TMyClass;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
 p.Free; //Getting an AV
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 p := TMyClass.Create;
end;

end.

Unit2: 单元2:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TMyThread = class(TThread)
  protected
   procedure Execute; override;
  end;

  TMyClass = class(TPersistent)
  private
   T: TMyThread;
  protected
   constructor Create;
  public
   destructor Destroy; override;
  end;


implementation

procedure TMyThread.Execute;
begin
 while not Self.Terminated do begin
  Sleep(1000);
  MessageBox(0, 'test', nil, MB_OK)
 end;
end;

constructor TMyClass.Create;
begin
 inherited Create;
 t := TMyThread.Create(False);
end;

destructor TMyClass.Destroy;
begin
 t.Terminate;
 t.WaitFor;
 FreeAndNil(t);
 inherited Destroy;
end;

end.

The constructor TMyClass.Create is declared protected. 构造函数TMyClass.Create声明为protected。 That means it is not visible from another unit. 这意味着从另一个单位看不到它。 Hence TMyClass.Create is not executed and instead you are calling TObject.Create . 因此, TMyClass.Create不会被执行,而是调用TObject.Create In turn that means that the thread is never created and you encounter a runtime error when the destructor runs because T is nil . 反过来,这意味着永远不会创建线程,并且在析构函数运行时遇到运行时错误,因为T nil

Declare the constructor with public visibility. 使用public可见性声明构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 delphi xe2正确处理从线程创建的指针,该指针正在发送到主线程 - delphi xe2 proper disposal of a pointer created from a thread which pointer is being sent to main thread Delphi:是否应该创建一个“未暂停”的线程? - Delphi: Should a thread ever be created “not suspended”? 在自定义线程中创建的对象在主线程上运行 - Objects created in custom Thread run on main Thread 启动画面是否从未创建的线程访问? - Splash screen being accessed from thread it was not created on? 将调用者线程的stacktrace附加到正在创建的新线程中,以便于调试 - Append stacktrace of caller thread into the new thread being created, for easier debugging 在单独的线程中创建(并显示)后,窗口立即关闭 - Window closes immediately after being created (and shown) in a sepatarted thread 在Web服务调用中创建子线程后停止 - Child thread stops after being created within a web service call 在挂起模式下创建的Delphi线程在Windows 2012 Server上自行启动 - Delphi thread created in suspended mode gets started by itself on windows 2012 server java-将新创建的线程附加到我的自定义ThreadGroup时的侦听器 - java - listener when new created thread attached to my custom ThreadGroup Delphi中的线程错误 - Thread error in delphi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM