为什么这个线程引发异常?
Why this thread raise exception?
我正在尝试在示例项目中创建一个线程,但我在示例项目代码中遇到了异常
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TURLDownload = class(TThread)
private
FURL: String;
Fnameofimg: string;
FPathImage: string;
FFileNameImage: string;
// Internal //
ImageName: string;
PathURL: string;
protected
procedure Execute; override;
public
constructor Create(const AUrl: String; Const AOutPathImages: string;
Anameofimg: String); reintroduce;
destructor Destroy; override;
property URL: string read FURL write FURL;
property PathImage: string read FPathImage;
property FileNameImage: string read FFileNameImage;
end;
var
Form1: TForm1;
th: TURLDownload;
implementation
{$R *.dfm}
{ TURLDownload }
procedure TURLDownload.reached;
begin
showmessage('done');
end;
constructor TURLDownload.Create(const AUrl, AOutPathImages: string;
Anameofimg: String);
begin
inherited Create(False);
FreeOnTerminate := True;
FURL := AUrl;
Fnameofimg := Anameofimg;
FPathImage := AOutPathImages;
end;
destructor TURLDownload.Destroy;
begin
inherited;
end;
procedure TURLDownload.Execute;
begin
synchronize(reached);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
th.Create('jgvjk', 'ngkj', 'jkgfjk');
end;
end.
当我点击 button1 开始创建线程时,我遇到了这条异常消息
First chance exception at [=12=]4C0384. Exception class $C0000005 with
message 'access violation at 0x004c0384: read of address 0x0000003c'.
Process Project1.exe (4060)
然后当我点击打破它 return 我到系统 类 线程内的文件创建在此代码
FSuspended := not FExternalThread;
我做错了什么?我正在使用 Delphi xe7
您应该使用
创建线程对象
th := TURLDownload.Create('jgvjk', 'ngkj', 'jkgfjk');
其他问题:
在您的线程主体中,您使用 showmessage('Reached');
调用 VCL window,但没有同步。
如果没有某种同步,您不应该与 VCL 人员一起工作 - 使用同步或队列。
reintroduce
不需要非虚拟构造函数
inherited
在 Execute
中什么都不做
我正在尝试在示例项目中创建一个线程,但我在示例项目代码中遇到了异常
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TURLDownload = class(TThread)
private
FURL: String;
Fnameofimg: string;
FPathImage: string;
FFileNameImage: string;
// Internal //
ImageName: string;
PathURL: string;
protected
procedure Execute; override;
public
constructor Create(const AUrl: String; Const AOutPathImages: string;
Anameofimg: String); reintroduce;
destructor Destroy; override;
property URL: string read FURL write FURL;
property PathImage: string read FPathImage;
property FileNameImage: string read FFileNameImage;
end;
var
Form1: TForm1;
th: TURLDownload;
implementation
{$R *.dfm}
{ TURLDownload }
procedure TURLDownload.reached;
begin
showmessage('done');
end;
constructor TURLDownload.Create(const AUrl, AOutPathImages: string;
Anameofimg: String);
begin
inherited Create(False);
FreeOnTerminate := True;
FURL := AUrl;
Fnameofimg := Anameofimg;
FPathImage := AOutPathImages;
end;
destructor TURLDownload.Destroy;
begin
inherited;
end;
procedure TURLDownload.Execute;
begin
synchronize(reached);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
th.Create('jgvjk', 'ngkj', 'jkgfjk');
end;
end.
当我点击 button1 开始创建线程时,我遇到了这条异常消息
First chance exception at [=12=]4C0384. Exception class $C0000005 with message 'access violation at 0x004c0384: read of address 0x0000003c'. Process Project1.exe (4060)
然后当我点击打破它 return 我到系统 类 线程内的文件创建在此代码
FSuspended := not FExternalThread;
我做错了什么?我正在使用 Delphi xe7
您应该使用
创建线程对象
th := TURLDownload.Create('jgvjk', 'ngkj', 'jkgfjk');
其他问题:
在您的线程主体中,您使用 showmessage('Reached');
调用 VCL window,但没有同步。
如果没有某种同步,您不应该与 VCL 人员一起工作 - 使用同步或队列。
reintroduce
不需要非虚拟构造函数
inherited
在 Execute