Что я делаю не так? При заходе в конструктор потока
сообщение выскакивает, но следующая команда приводит к
"Access violation..."
Подробности вот:
1. File\New\Application
2. Кинул кнопку на форму
3. код формы
------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
end;
var
Form1: TForm1;
CopyThread: TCopyThread;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
CopyThread.Create(False);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if not (CopyThread = nil) then begin
CopyThread.Terminate;
end;
end;
end.
------------------------------
4. Добавил еще один модуль (Unit2)
вот код модуля
------------------------------
unit Unit2;
interface
uses
Classes, Dialogs, Windows;
type
TCopyThread = class(TThread)
protected
procedure Execute; override;
public
constructor Create(S: String);
destructor Destroy; override;
end;
implementation
constructor TCopyThread.Create(S: String);
begin
showmessage(S);
inherited Create(False);
end;
destructor TCopyThread.Destroy;
begin
inherited Destroy;
end;
procedure TCopyThread.Execute;
begin
sleep(1000);
end;
end.
------------------------------