为什么此线程引发异常? [英] Why this thread raise exception?

查看:86
本文介绍了为什么此线程引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在示例项目中创建一个线程,但这里引发了一个异常,即示例项目代码

i am trying to create a thread in sample project but i got an exception raised here is the sample project code

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开始创建线程时,我被此异常消息卡住

when i click on button1 to start creating Thread i stuck with this exception message

$ 004C0384的第一次机会例外.具有$ C0000005的异常类 消息'访问冲突在0x004c0384:读取地址0x0000003c'. 处理Project1.exe(4060)

First chance exception at $004C0384. Exception class $C0000005 with message 'access violation at 0x004c0384: read of address 0x0000003c'. Process Project1.exe (4060)

然后当我单击中断时,将我返回到在此代码创建的线程内的系统类文件

and then when i click break its return me to system classes file inside thread create at this code

  FSuspended := not FExternalThread;

我做错了什么?我正在使用Delphi xe7

what i am doing wrong ? i am using Delphi xe7

推荐答案

您应使用
创建线程对象 th := TURLDownload.Create('jgvjk', 'ngkj', 'jkgfjk');

You should create thread object with
th := TURLDownload.Create('jgvjk', 'ngkj', 'jkgfjk');

另一个问题:

在线程体内,您可以使用showmessage('Reached');调用VCL窗口,而无需同步. 如果没有某种同步,则不应与VCL工作人员一起使用-使用同步"或队列".

In your thread body you call VCL window using showmessage('Reached'); without synchronization. You should not work with VCL staff without some kind of synchronization - use Synchronize or Queue.

reintroduce

inheritedExecute

这篇关于为什么此线程引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆