我正在尝试在示例项目中创建一个线程,但我得到的一个例外是示例项目代码
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开始创建线程时我遇到了这个异常消息
$ 004C0384的第一次机会异常。异常类$ C0000005 with 消息'访问冲突0x004c0384:读取地址0x0000003c'。 处理Project1.exe(4060)
然后当我点击break它返回我在这个代码的线程创建中的系统类文件
FSuspended := not FExternalThread;
我做错了什么?我正在使用Delphi xe7
答案 0 :(得分:3)
你应该用创建线程对象
th := TURLDownload.Create('jgvjk', 'ngkj', 'jkgfjk');
另一个问题:
在线程主体中,使用showmessage('Reached');
调用VCL窗口而不进行同步。
如果没有某种同步,您不应该使用VCL工作人员 - 使用Synchronize或Queue。
reintroduce
inherited
在Execute