unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Edit2: TEdit;
    Label2: TLabel;
    Button3: TButton;
    Button4: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button3Click(Sender: TObject);
begin
  if Self.OpenDialog1.Execute then begin
    GLOBAL_POSITION:= 0;
    Edit1.Text:= Self.OpenDialog1.FileName;
    Button1.Enabled:= True;
    Button2.Enabled:= False;
    Button3.Enabled:= False;
    Button4.Enabled:= False;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  GLOBAL_TASK:= TUploadFile.Create(Edit2.Text, Edit1.Text);
  try
    GLOBAL_TASK.Resume;
    Button1.Enabled:= False;
    Button2.Enabled:= True;
    Button3.Enabled:= False;
    Button4.Enabled:= True;
  except
    ListBox1.Items.Add('启动文件上传错误，无法启动线程');
    Button1.Enabled:= False;
    Button2.Enabled:= False;
    Button3.Enabled:= True;
    Button4.Enabled:= False;
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  if Assigned(GLOBAL_TASK) then begin
    try
      GLOBAL_TASK.Cancel;
      Button1.Enabled:= False;
      Button2.Enabled:= False;
      Button3.Enabled:= True;
      Button4.Enabled:= False;
    except
      ListBox1.Items.Add('取消文件上传错误，无法取消线程');
      Button1.Enabled:= False;
      Button2.Enabled:= False;
      Button3.Enabled:= True;
      Button4.Enabled:= False;
    end;
  end
  else begin
    ListBox1.Items.Add('取消文件上传成功');
    Button1.Enabled:= False;
    Button2.Enabled:= False;
    Button3.Enabled:= True;
    Button4.Enabled:= False;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if Assigned(GLOBAL_TASK) then begin
    try
      GLOBAL_TASK.Pause;
      Button1.Enabled:= True;
      Button2.Enabled:= False;
      Button3.Enabled:= False;
      Button4.Enabled:= True;
    except
      ListBox1.Items.Add('暂停文件上传错误，无法暂停线程');
      Button1.Enabled:= False;
      Button2.Enabled:= False;
      Button3.Enabled:= True;
      Button4.Enabled:= False;
    end;
  end
  else begin
    ListBox1.Items.Add('暂停文件上传错误，文件上传已终止');
    Button1.Enabled:= False;
    Button2.Enabled:= False;
    Button3.Enabled:= True;
    Button4.Enabled:= False;
  end;
end;

end.
