Question

I'm using Delphi 2006 and Indy 10. I create a form and drop down an IdHttpServer component. I make an OnCreate event for the form to set the server active, and I enter these lines for the server's OnCommandGet:

procedure TForm3.IdHTTPServerCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
   Beep;
   Sleep(10000);
   AResponseInfo.ContentText := DateTimeToStr(Now);
end;

Note the Sleep for 10 seconds.

I then test with Firefox, using 2 browsers. I have the first one connect to "localhost", and I hear a beep right away. I then tab to the 2nd browser, and have it connect to localhost (in less than 10 seconds), but it doesn't beep right away. It waits for the 1st request to complete, then beeps, and waits another 10 seconds.

I thought these components were multi-threaded? Is there some property I can set to make it behave the way I thought it would (both requests would get answered immediately).

Was it helpful?

Solution

Not Indy and the TIdHTTPServer is responsible for this behaviour! It's the webbrowser!

Firefox shares the TCP connection for different requests at the same server.

So, Firefox serializes the 2 requests for the same URI. Open 2 different browsers at the same time (e.g. IE and Firefox), request http://localhost/ in both and you will get the expected result.

And the answer to your question: Yes, of course, every TIdHTTPServer.OnCommandGet event is executed in an own "scheduler" thread, and can be executed simultaneously.

OTHER TIPS

GUI is responsive during this 10 seconds, so it is multiThreaded for long operations put your code into another thread - and you will get what you want

unit uSomeThread;

interface

uses
  System.Classes;

type
  TSomeThread = class(TThread)
  protected
    procedure Execute; override;
  end;

implementation


procedure TSomeThread.Execute;
begin
  //  Beep;
  Sleep(10000);
end;

end.

...........

    procedure TServer.IdHTTPServerCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  cContext: TClientContext;
  t: TSomeThread;
begin
//  Beep;
//  Sleep(10000);

  t := TSomeThread.Create(true);
  t.FreeOnTerminate := true;
  t.Start;

  AResponseInfo.ResponseNo := 200;
  AResponseInfo.CacheControl := 'no-cache';
  AResponseInfo.CustomHeaders.Add('Access-Control-Allow-Origin: *');
  AResponseInfo.ContentText := 'ok';
  AResponseInfo.ResponseNo := 200;
  AResponseInfo.WriteContent;
  Beep;

end;

I have used Indy 10 idHTTPServer and it is multithread. What blocking your app might be the "beep" or the "sleep" command. Because although the component is multithread, some commands may still lock the whole process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top