Access Violation with TIdHttp Running on Many Threads at Once

I have been trying for some time to make an "angry http downloader" in delphi, but TIdHttpCli just can't do what I want. For some reason, it will not start simultaneously in many threads. Please take a look at the simplicity of demonstrating this problem:

procedure HttpRequest(AParam : Integer); stdcall;
var
  lHttp: TIdHttp;
begin
  lHttp := TIdHttp.Create(nil);
  {
  lHttp.Get(
    'http://stackoverflow.com/questions/15977505/',
    TMemoryStream.Create
  );
  }
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  tid: DWORD;
begin
  for i := 0 to 4 do
    CreateThread(nil, 0, @HttpRequest, nil, 0, tid);
end;

David Heffernan: I simplified the code in the question. This code still demonstrates behavior. My test environment was XE3 with Indy, which was delivered with XE3.

+5
source share
1 answer

. , , IsMultiThread True. , TThread.

:

IsMultiThread True, , . IsMultiThread True BeginThread .

Windows API CreateThread, RTL , IsMultiThread True. , . , , .

IsMultiThread := True , . TThread.

, . . :

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;

function HttpRequest(AParam : Integer): DWORD; stdcall;
var
  i: Integer;
  P: Pointer;
begin
  Result := 0;
  for i := 1 to 100000 do
    GetMem(P, 1);
end;

var
  i: Integer;
  tid: DWORD;

begin
  try
    //IsMultiThread := True;//include this line to make program correct
    for i := 0 to 15 do
      CreateThread(nil, 0, @HttpRequest, nil, 0, tid);
  except
    on E:Exception do
      Writeln(E.Message);
  end;
  Readln;
end.
+8

All Articles