我以下创建多线程的代码,引起如下图运行时错误,请各位前辈指点。谢谢!procedure TForm1.Button1Click(Sender: TObject);begin
    RunThread(10);
end;procedure ThreadFunction();begin
    html.Add(Form1.IdHTTP1.Get('http://iecms.ec.com.cn/iecms/corp/Corp_query_list.jsp?toPageNo=10&corp_code=&corp_name=福建&recordCount=3762'));
end;procedure RunThread(pageCount:Integer);
var
    i:Integer;
    ThreadID:DWORD;
begin
    for i:=1 to pageCount do begin
        CreateThread(nil,0,@ThreadFunction,nil,0,ThreadID);
    end;
    ShowMessage('run all!');
end;

解决方案 »

  1.   

    加临界,否则频繁Form1.IdHTTP1.Get会发生冲突
      

  2.   

    html是什么?Twebbrowser?
    form1.idhttp1能同时访问不同的网页?
      

  3.   

    这是线程同步问题,主窗体中的对象,是在主线程中使用的,你自己创建的线程,需要使用你自己创建的对象来操作http
      

  4.   

    demo1...
    unit Unit2;interfaceuses
      Classes,ADODB,SysUtils,ActiveX;
    type
      Ta = class(TThread)
      constructor Create(ado:Tadoquery;s:string);
    destructor Destroy;override;
      private
      a:Tadoquery;
      ss:string;
        { Private declarations }
      protected
        procedure Execute; override;
      end;implementation
    uses unit1;
    { Important: Methods and properties of objects in VCL or CLX can only be used
      in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure Ta.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ Ta }
    constructor Ta.Create(ado:Tadoquery;s:string);
    begin
    inherited create(true);
    FreeOnTerminate:=true;
    a:=ado;
    ss:=s;
    Resume;
    end;
    procedure Ta.Execute;
    begin
    CoInitialize(nil);//必须的
    sleep(5000);
    a.SQL.Add(ss);
    try
    a.Open;
    except
    raise
    end;
    end;
    destructor Ta.Destroy;
    begin
    a:=nil;
    inherited Destroy;
    end;
    主窗体的调用
    var t:Ta;
    begin
    t:=Ta.Create(adoquery1,'select * from 成绩');
    demo2...
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);  private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      threadid:dword;
      hthread:thandle;
      procedure tt();implementation{$R *.dfm}procedure  tt();//线程要执行的函数
    var i:integer;
    begin
    for i:=0 to 20000 do
    form1.Edit1.Text:=inttostr(i);
    end;procedure TForm1.Button1Click(Sender: TObject);//
    begin
    hthread:=createthread(nil,0,@tt,nil,0,threadid); //创建线程并立即执行
    if hthread=0 then
    messagebox(handle,'创建失败',nil,mb_ok);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    suspendthread(hthread);//挂起线程
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
    resumethread(hthread);//恢复线程
    end;
    procedure TForm1.Button4Click(Sender: TObject);
    begin
    terminatethread(hthread,2);//结束线程
    end;
    end.