我在delphi中遇到多线程问题。我有一个名称列表(约2000个名字),我需要在我的网站上获取每个名字的一些数据。除了线程控制外,我的系统完美运行。多线程Delphi
我想创建10个线程,并且,当某个线程终止时,创建另一个......直到列表结束。
var
Form1: TForm;
tCount: Integer; //threads count
implementation
type
TCheck = class(TThread)
public
constructor Create(Name: string);
destructor Destroy; Override;
protected
procedure Execute; Override;
end;
MainT = class(TThread)
protected
procedure Execute; Override;
end;
destructor TCheck.Destroy;
begin
Dec(tCount);
end;
procedure MainT.Execute;
var
i: Integer;
Load: TStringList;
begin
Load:=TStringList.Create;
Load.LoadFromFile('C:\mynames.txt');
for i:= 0 to Load.Count -1 do
begin
if tCount = 10 then //if we have 10 threads running...
begin
repeat
Sleep(1);
until tCount < 10;
end;
TCheck.Create(Load.Strings[i]);
TCheck.Start;
Inc(tCount);
end;
end; // end of procedure
嗯,我没有把TCheck.Constructor因为这个问题是我是如何检查创建的线程数量的方法。我的意思是,我的软件只是停止,没有任何错误信息,有时检查500名,有时150名...
对不起。
你可以发布TCheck ctor吗? – 2013-03-25 18:39:51
另外,如果您想要10个线程,请制作10个线程,并让他们通过排队等待所有工作来处理您的工作。不要不断地创建/终止/销毁它们。忘掉tCount和微管理线程。 – 2013-03-25 18:42:07
是的。你需要一个填充线程安全队列的生产者,以及10个消耗它的用户。 – 2013-03-25 19:31:53