把10个url由http.get提交到服务器,用多线程的话怎么写代码? 单线程的我会,可是多线程的就不会啦。这10个URL,我想用2个线程来提交,前5个URL由第一个线程提交,后5个URL由第二个线程提交。具体的代码怎么写呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你用一个StringList保存下前五个地址交给第一个线程处理,线程再一个个依序处理,第二个线程类似,不就好了? //没做测试,仅为示意代码,LZ可自行修改一下//线程类unit Unit2;interfaceuses Classes,SysUtils;type TTestThread = class(TThread) private { Private declarations } FURLList:TStringList; FStartPos,FEndPos:Integer; procedure PostWeb; protected procedure Execute; override; public property StartPos:Integer read FStartPos write FStartPos; property EndPos :Integer read FEndPos write FEndPos; constructor Create(CreateSuspended: Boolean;URLList:TStringList); destructor Destroy; override; end;implementationconstructor TTestThread.Create(CreateSuspended: Boolean;URLList:TStringList);begin inherited Create(CreateSuspended); FURLList:=URLList;end;destructor TTestThread.Destroy;begin FreeAndNil(FURLList); inherited;end;procedure TTestThread.PostWeb ;var I:Integer;begin for I:=StartPos to EndPos do begin ////URLList ///////////////此处添写处理的URL end;end;procedure TTestThread.Execute;begin FreeOnTerminate:=True; PostWeb; { Place thread code here }end;end.//主程序procedure TForm1.Button1Click(Sender: TObject);var Test:TTestThread; URLList:TStringList;begin URLList:=TStringList.Create; URLList.Add('URL1'); URLList.Add('URL2'); URLList.Add('URL3'); Test:=TTestThread.Create(True,URLList);//创建一个线程,若有多个线程,依次创建 Test.StartPos:=1; //网址在StringList中的起始位置 Test.EndPos:=2; //网址在StringList中的结束位置 Test.Resume;end; 实在对不起LZ,多线程有点问题~应该在主程序中将TStringList传入子线程后,将TStringList进行Clone,否则第一个线程执行完后,第二个线程就出AV错误了:) 感谢这位老兄的答复,基本知道多线程怎么搞了。只是不明白 “应该在主程序中将TStringList传入子线程后,将TStringList进行Clone” 这个应该在主程序里怎么写呢?为什么要这么做? 我是想让你在主程序界面上录入URL,以StringList的形式传到线程中,在线程中用Assign来复制一下主程序传来的对象,明白? 创建了2个线程 test1 := TTestThread.Create(True,URLListEnd); test1.StartPos :=0; test1.EndPos :=2; test1.Resume; test2 := TTestThread.Create(True,URLListEnd); test2.StartPos :=4; test2.EndPos :=5; test2.Resume;在procedure TTestThread.PostWeb大概是这样idhttp.get(URLListEnd.strings[i]);--------------------------------------------现在单独执行第一个线程,正常;单独执行第二个线程也正常;2个线程都执行的话,就出错了。 还有,如果创建10个线程,怎么写? test1 := TTestThread.Create(True,URLListEnd); test1.StartPos :=0; test1.EndPos :=2; test1.Resume; test2 := TTestThread.Create(True,URLListEnd); test2.StartPos :=4; test2.EndPos :=5; test2.Resume; 这样重复下去?有没有其他写法? 创建了2个线程 test1 := TTestThread.Create(True,URLListEnd); test1.StartPos :=0; test1.EndPos :=2; test1.Resume; test2 := TTestThread.Create(True,URLListEnd); test2.StartPos :=4; test2.EndPos :=5; test2.Resume; 在procedure TTestThread.PostWeb大概是这样 idhttp.get(URLListEnd.strings[i]); TTestThread.Execute是这样 FreeOnTerminate:=True; PostWeb; -------------------------------------------- 现在单独执行第一个线程,正常;单独执行第二个线程也正常; 2个线程都执行的话,就出错了。 2个线程同时执行就出错,是不是TTestThread.Execute是这样 FreeOnTerminate:=True; PostWeb; 这里要写什么啊? 对话框居中显示 maskedit listbox 无情的雨,无情的你,无情地抢走了马夹仅有的60分-_-!!! 有谁用过三方的DBGridEH吗?如何把Footer(脚注)调高 改变MDI窗口的排列顺序 报表问题???? 高手请进,一个系统架构的问题? SP1 到哪里下载? 将Activex控件插入到网页中,在关闭网页时经常出现iexplore出错,为什么啊? 如何分别封锁Ctrl+Alt+Del和其他的快捷键(如Alt+Tab) 這是什么类型?有没有人答啊啦! 如何获取 Microsoft Web Browser 控件里的网页内容?
//线程类unit Unit2;interfaceuses
Classes,SysUtils;type
TTestThread = class(TThread)
private
{ Private declarations }
FURLList:TStringList;
FStartPos,FEndPos:Integer;
procedure PostWeb;
protected
procedure Execute; override;
public
property StartPos:Integer read FStartPos write FStartPos;
property EndPos :Integer read FEndPos write FEndPos;
constructor Create(CreateSuspended: Boolean;URLList:TStringList);
destructor Destroy; override; end;implementationconstructor TTestThread.Create(CreateSuspended: Boolean;URLList:TStringList);
begin
inherited Create(CreateSuspended);
FURLList:=URLList;
end;destructor TTestThread.Destroy;
begin
FreeAndNil(FURLList);
inherited;
end;procedure TTestThread.PostWeb ;
var
I:Integer;
begin
for I:=StartPos to EndPos do
begin
////URLList
///////////////此处添写处理的URL
end;
end;procedure TTestThread.Execute;
begin
FreeOnTerminate:=True;
PostWeb; { Place thread code here }
end;end.
//主程序
procedure TForm1.Button1Click(Sender: TObject);
var
Test:TTestThread;
URLList:TStringList;
begin
URLList:=TStringList.Create;
URLList.Add('URL1');
URLList.Add('URL2');
URLList.Add('URL3'); Test:=TTestThread.Create(True,URLList);//创建一个线程,若有多个线程,依次创建
Test.StartPos:=1; //网址在StringList中的起始位置
Test.EndPos:=2; //网址在StringList中的结束位置
Test.Resume;end;
应该在主程序中将TStringList传入子线程后,将TStringList进行Clone,否则第一个线程执行完后,第二个线程就出AV错误了:)
感谢这位老兄的答复,基本知道多线程怎么搞了。
只是不明白 “应该在主程序中将TStringList传入子线程后,将TStringList进行Clone” 这个应该在主程序里怎么写呢?为什么要这么做?
test1.StartPos :=0;
test1.EndPos :=2;
test1.Resume; test2 := TTestThread.Create(True,URLListEnd);
test2.StartPos :=4;
test2.EndPos :=5;
test2.Resume;在procedure TTestThread.PostWeb大概是这样idhttp.get(URLListEnd.strings[i]);--------------------------------------------现在单独执行第一个线程,正常;单独执行第二个线程也正常;
2个线程都执行的话,就出错了。
test1.StartPos :=0;
test1.EndPos :=2;
test1.Resume; test2 := TTestThread.Create(True,URLListEnd);
test2.StartPos :=4;
test2.EndPos :=5;
test2.Resume;
这样重复下去?有没有其他写法?
test1.StartPos :=0;
test1.EndPos :=2;
test1.Resume; test2 := TTestThread.Create(True,URLListEnd);
test2.StartPos :=4;
test2.EndPos :=5;
test2.Resume; 在procedure TTestThread.PostWeb大概是这样
idhttp.get(URLListEnd.strings[i]); TTestThread.Execute是这样
FreeOnTerminate:=True;
PostWeb;
-------------------------------------------- 现在单独执行第一个线程,正常;单独执行第二个线程也正常;
2个线程都执行的话,就出错了。
FreeOnTerminate:=True;
PostWeb; 这里要写什么啊?