刚学编程,先说明一下我想做什么:我想在form上安两个按钮,当按下第一个按钮的时候触发构建一个循环的线程(unit1),然后按下第二个按钮的时候,不论执行到什么地方,我希望通过全局变量thrd1来触发循环退出从而结束这个线程。再按下第一个按钮的时候,重新启动那个循环线程(一定得从头来)如此循环。下面是我的代码,其中循环的程序我用了简单的几个代码来代替。unit form;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Unit1;type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }  end;var
  Form1: TForm1;
  thread1 : mymath1;   
  thrd1:boolean;implementation{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
  if thread1=nil then
  begin
  thrd1:=true;
  thread1:=mymath1.create;
  sleep(500);
  thread1.resume;
  end;end;procedure TForm1.Button2Click(Sender: TObject);
begin
   if thread1<>nil then
   begin
   thrd1:=false;
   sleep(20);
   thread1.destroy;
   end;
end;end.unit Unit1;interfaceuses
  Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;type
  mymath1 = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;  public      
    constructor create;   
    destructor destroy;
  end;implementation{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure mymath1.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ mymath1 }uses form;constructor mymath1.create;   
begin 
  inherited create(true);
end;destructor mymath1.destroy;
begin
   inherited destroy;
end;
 
procedure mymath1.Execute;
var
i : integer;
begin
  { Place thread code here }
    for i := 1 to 1000 do
    begin
       if thrd1 then
       begin
           showmessage('the num is:'+ inttostr(i));
           Sleep(2000);
       end
       else
       exit;
    end;
end;
end.我现在碰到的问题是:
1。按下第一个按钮的时候马上会出现差不多半个屏幕的窗体,关闭这个窗体之后,从“the num is 2"开始循环;
2、有些时候循环到某 一个数(比如是10)的时候,信息框会变得比正常显示的时候稍微大一点点的信息框,但是上面却只有"确定"按钮,确定之后从11开始循环。
3、我的本意是在用button2停止之后,再按button1,还能够重新启动循环。但是现在按button2之后,就无法重新启动循环了。
4、如果按了button2之后,再按button1,就会出现运行的exe文件"没有反应"的状态,报错
  "project project1.exe raised exception class EAccessViolation with message'Acess violation at address 00000000.write of address 00000000'  process stopped . use step or run to continue.主要就是以上问题,请高手帮我指点指点,问题出在什么地方。