我有一个来自 TThread 的孩子。一切正常,但我怎样才能对我创建的线程进行大规模挂起或恢复?或者我怎样才能只挂起第二个线程(在 Button2Click 中创建)?这是我的代码的一部分:
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(如第一个代码示例)做到这一点?