如何(控制)循环开启线程?(200)

  • 主题发起人 主题发起人 newester
  • 开始时间 开始时间
N

newester

Unregistered / Unconfirmed
GUEST, unregistred user!
如何(控制)循环开启线程(即某个线程运行完毕,立即检测出来并开启一个新线程运行)呢 ? 就是总能保证有( x= 5...)个线程在运行。 我弄一天了,还没弄好;想弄个采集数据的简单工具,大歌歌们,教教我 。。。 最好能给个实例我学习一下。谢谢谢谢 ~o~ ... 下面是部分代码: ................................... // aUrlList 局部TStringList变量,保存有N个Url链接地址 Threads:=0;
// 全局变量,用来纪录正在运行的线程总数; nThread:=0;
// 局部变量,用来纪录已经开启的线程总数; // ( 下面想实现程序总是有5个线程同时运行,当然前提是aUrlList中的链接地址足够多 ) while truedo
begin
if nThread >= aUrlList.count then
begin
break;
end else
begin
if Threads < 5 then
// ( 这里这样读取全局变量,肯定有误,如何处理呢?我搞不清楚 ... ) begin
New(MyPara);
// MyPara为 record类型 MyPara^.Url := aUrlList[nThread];
CreateThread(nil, 0, @aThreadRun, MyPara, 0, ID);
nThread:=nThread+1;
Threads:=Threads+1;
// ( 这里这样操作全局变量,肯定有误,如何处理呢?我搞不清楚 ... ) end;
end;
Application.ProcessMessages;
end;
repeat Application.ProcessMessages;
until ( Threads = 0 );
................................... 上面操纵全局变量 Threads 时,我在其前面加 EnterCriticalSection(CS),其后加 LeaveCriticalSection(CS)时,结果程序也会出问题;怎么弄呢? ----------------------------- 分隔线,下面是 aThreadRun函数 内部分代码 -------------------------------- function aThreadRun(p:xParameter): DWORD;
stdcall;
...................... try try ............................... .... 部分处理网页源码的code ... ............................... EnterCriticalSection(CS);
Writeln(oFile,tmp);
Result := 1;
exit;
except end finally begin
...................... Threads := Threads - 1;
LeaveCriticalSection(CS);
ExitThread(0);
end;
.....................................
 
你可以开5个线程,让他们自己找活干,[8D]
 
"你可以开5个线程,让他们自己找活干 ... " 哥歌,太言简意赅了 ... 我没什么基础,接受不了 ... 详细点说说嘛 [:)] 是不是开始时一次开5个线程,然后5个线程中的每个线程在结束时,再开新的线程吗?
 
用循环控制线程,不安全吧,可以用sleep 停止一个线程,在开启令一个线程,本来线程就是轮询的,按时间片走的,你为什么要弄5个啊,一个不够么
 
到cnPack上下在他们的源代码。里面有个cnThreadPool,保证能满足你的要求。设置ThreadMinCount为5即可。
 
////////////////////////////////////// 作者:fy.wang // // 利用互斥实现线程同步,以及线 // // 程函数指针参数的传递 // //////////////////////////////////// unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ADODB, frxClass, frxDBSet, StdCtrls, RzButton, RzEdit, RzPrgres, RzCommon, Mask, ExtCtrls, RzPanel, RzTabs;type My = record MyThreadName: string;
end;
PMy = ^My;
TForm1 = class(TForm) RzPageControl1: TRzPageControl;
RzFrameController1: TRzFrameController;
TabSheet1: TRzTabSheet;
RzMemo1: TRzMemo;
RzPanel1: TRzPanel;
RzButton1: TRzButton;
RzButton2: TRzButton;
RzButton3: TRzButton;
RzButton4: TRzButton;
RzProgressBar1: TRzProgressBar;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure RzButton1Click(Sender: TObject);
procedure RzButton2Click(Sender: TObject);
procedure RzButton3Click(Sender: TObject);
procedure RzButton4Click(Sender: TObject);
private { Private declarations } public Stra, Ed: Int64;
{ Public declarations } end;
var Form1: TForm1;
MutexHandle: THandle;
ThreadHandle, ThreadHandle1, ThreadHandle2: THandle;
i: integer;implementationuses Unit2;{$R *.dfm}function ThreadFunc1(Info: Pointer): Integer;
stdcall;
//线程函数1begin
while i < 999do
begin
WaitForSingleObject(MutexHandle, INFINITE);
//等待互斥对象被释放 form1.rzMemo1.Lines.Add(PMy(info)^.MyThreadName + ' ' + IntToStr(i));
Inc(i);
form1.RzProgressBar1.Percent := Round((i / 999 * 100));
//驱动进程条 ReleaseMutex(MutexHandle);
end;
Dispose(Info);
//释放参数指针 CloseHandle(ThreadHandle);
//关闭线程对象 ThreadHandle := 0;
//初始化线程句柄 WaitForSingleObject(MutexHandle, INFINITE);
Form1.Ed := Windows.GetTickCount;
Form1.RzMemo1.Lines.Add('线程一用时:' + IntToStr(Form1.Ed - form1.Stra) + '豪秒!');
ReleaseMutex(MutexHandle);
end;
function ThreadFunc2(Info: Pointer): Integer;
stdcall;
//线程函数2begin
while i < 999do
begin
WaitForSingleObject(MutexHandle, INFINITE);
form1.rzMemo1.Lines.Add('Thread2 ' + IntToStr(i));
Inc(i);
form1.RzProgressBar1.Percent := Round((i / 999 * 100));
ReleaseMutex(MutexHandle);
end;
CloseHandle(ThreadHandle2);
ThreadHandle2 := 0;
WaitForSingleObject(MutexHandle, INFINITE);
Form1.Ed := Windows.GetTickCount;
Form1.RzMemo1.Lines.Add('线程二用时:' + IntToStr(Form1.Ed - form1.Stra) + '豪秒!');
ReleaseMutex(MutexHandle);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
i := 0;
MutexHandle := 0;
//初始互斥句柄end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if (ThreadHandle>0) and (ThreadHandle2>0 ) then
begin
MessageBox(Self.Handle,'线程在执行,请稍候再退出..','提示',MB_OK+MB_ICONWARNING);
Abort;
end;
CloseHandle(MutexHandle);
end;
procedure TForm1.RzButton1Click(Sender: TObject);var ThreadId1, ThreadId2: DWORD;
aMy: PMy;
begin
if (ThreadHandle2 <> 0) and (ThreadHandle <> 0) then
exit;
Stra := GetTickCount;
MutexHandle := CreateMutex(nil, False, 'MyMutex');
//建立互斥 New(aMy);
aMy.MyThreadName := 'Thread1';
ThreadHandle := CreateThread(nil, 0, @ThreadFunc1, aMy, 0, ThreadId1);
//建立线程 ThreadHandle2 := CreateThread(nil, 0, @ThreadFunc2, nil, 0, ThreadId2);
end;
procedure TForm1.RzButton2Click(Sender: TObject);
begin
SuspendThread(ThreadHandle);
SuspendThread(ThreadHandle2);
end;
procedure TForm1.RzButton3Click(Sender: TObject);
begin
ResumeThread(ThreadHandle);
ResumeThread(ThreadHandle2);
end;
procedure TForm1.RzButton4Click(Sender: TObject);
begin
SuspendThread(ThreadHandle);
//暂停线程 SuspendThread(ThreadHandle2);
CloseHandle(ThreadHandle2);
//关闭对象 CloseHandle(ThreadHandle);
ThreadHandle2 := 0;
//初始化句柄 ThreadHandle := 0;
CloseHandle(MutexHandle);
//释放互斥对象 MutexHandle := 0;
//初始化互斥句柄 rzMemo1.Lines.Clear;
i := 0;
RzProgressBar1.Percent := 0;
//初始化进度条end;
end.
这个一定可以满哫你的要求我这里是三个。你再加两个就行。
 
谢谢大家谢谢大家,已经解决了;用这个东东InterlockedIncrement(Threads)、LeaveCriticalSection(CS) 。。 再次感谢大家的帮助
 
后退
顶部