单线程的我会,可是多线程的就不会啦。
这10个URL,我想用2个线程来提交,前5个URL由第一个线程提交,后5个URL由第二个线程提交。
具体的代码怎么写呢?

解决方案 »

  1.   

    你用一个StringList保存下前五个地址交给第一个线程处理,线程再一个个依序处理,第二个线程类似,不就好了?
      

  2.   

    //没做测试,仅为示意代码,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;
      

  3.   

    实在对不起LZ,多线程有点问题~
    应该在主程序中将TStringList传入子线程后,将TStringList进行Clone,否则第一个线程执行完后,第二个线程就出AV错误了:)
      

  4.   


    感谢这位老兄的答复,基本知道多线程怎么搞了。
    只是不明白  “应该在主程序中将TStringList传入子线程后,将TStringList进行Clone” 这个应该在主程序里怎么写呢?为什么要这么做?
      

  5.   

    我是想让你在主程序界面上录入URL,以StringList的形式传到线程中,在线程中用Assign来复制一下主程序传来的对象,明白?
      

  6.   

    创建了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个线程都执行的话,就出错了。
      

  7.   

    还有,如果创建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; 
    这样重复下去?有没有其他写法?
      

  8.   

    创建了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个线程都执行的话,就出错了。
      

  9.   

    2个线程同时执行就出错,是不是TTestThread.Execute是这样 
      FreeOnTerminate:=True; 
      PostWeb; 这里要写什么啊?