TThreadの子供がいます。すべて正常に動作しますが、作成したスレッドを一括中断または再開するにはどうすればよいですか?または、(Button2Clickで作成された)2番目のスレッドのみを一時停止するにはどうすればよいですか?これが私のコードの一部です:
TMyThread = class(TThread)
private
source_file, destination_file: string;
total_size, current_size, download_item_id: integer;
protected
procedure ShowResult;
procedure Execute; override;
public
end;
var
MyThread: TMyThread;
begin
procedure TMyThread.Execute;
begin
//Some code for download file here, it doesn't matter
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MyThread := TMyThread.Create(True);
MyThread.source_file :='http://example.com/download1.zip';
MyThread.destination_file := 'c:\download1.zip';
MyThread.download_item_id := 0;
MyThread.Priority := tpNormal;
MyThread.FreeOnTerminate := True;
MyThread.Resume;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MyThread := TMyThread.Create(True);
MyThread.source_file :='http://example.com/download2.zip';
MyThread.destination_file := 'c:\download2.zip';
MyThread.download_item_id := 1;
MyThread.Priority := tpNormal;
MyThread.FreeOnTerminate := True;
MyThread.Resume;
end;
end.
つまり、このようなスレッドを作成すると、次のように機能します。
var
MyThread1, MyThread2: TMyThread;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
MyThread1 := TMyThread.Create(True);
MyThread1.source_file :='http://example.com/download1.zip';
MyThread1.destination_file := 'c:\download1.zip';
MyThread1.download_item_id := 0;
MyThread1.Priority := tpNormal;
MyThread1.FreeOnTerminate := True;
MyThread1.Resume;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MyThread2 := TMyThread.Create(True);
MyThread2.source_file :='http://example.com/download2.zip';
MyThread2.destination_file := 'c:\download2.zip';
MyThread2.download_item_id := 1;
MyThread2.Priority := tpNormal;
MyThread2.FreeOnTerminate := True;
MyThread2.Resume;
end;
//Terminate all of TMyThread
procedure TForm1.Button3Click(Sender: TObject);
begin
MyThread1.Terminate;
MyThread2.Terminate;
ShowMessage('All downloads were terminated!');
end;
//Terminate ONLY the second of TMyThread
procedure TForm1.Button4Click(Sender: TObject);
begin
MyThread2.Terminate;
ShowMessage('The second download was terminated!');
end;
しかし、(最初のコードサンプルのように)動的に作成されたTMyThreadのセットに対してそれを行うにはどうすればよいですか?