title: How to implement long running queries on IdhttpServer?
body: <p>What is good way to implement long running queries on IdHttpServer. I have written simple logic to do so, please advise suggest better way to achive the same as I'm struggling with its performance. 
  I am using D2010 and Indy 10.5.8 to achieve the goal, also suggest if we retrive values frequently from session will that be a resource intensive ?</p>
  
  <pre><code>procedure TForm1.ServerCommandGet(AContext: TIdContext;
    ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  var
     SessionObj : TSessionData;
  begin
   if ARequestInfo.Document = 'EXECUTEQUERY' then
   begin
     if not Assigned(ARequestInfo.Session.Content) then
     begin
        SessionObj := TSessionData.Create;
        ARequestInfo.Session.Content.AddObject('U_SESSION', SessionObj);
        SessionObj.RunLongQuery;
     end;
   end;
  
   if ARequestInfo.Document = 'GETDATA' then
   begin
     SessionObj := TSessionData(ARequestInfo.Session.Content.Objects[ ARequestInfo.Session.Content.IndexOf('U_SESSION')]);
     if SessionObj.GetQueryStat = Done  then
      begin
         AResponseInfo.ContentStream.CopyFrom(SessionObj.GetMemStream, SessionObj.GetMemStream.Size);
         SessionObj.GetMemStream.Clear;
         AResponseInfo.ResponseNo := 200;
      end else if SessionObj.GetQueryStat = Error then
          AResponseInfo.ResponseNo := 500
      else AResponseInfo.ResponseNo := 102;
   end;
  
  end;
  
  procedure TForm1.ServerSessionEnd(Sender: TIdHTTPSession);
  begin
    TSessionData(Sender.Content.Objects[ Sender.Content.IndexOf('U_SESSION')]).Free;
  end;
  
  { TProcessQuery }
  
  constructor TProcessQuery.Create;
  begin
    myConn := TMyConnection.Create(nil);
    myConn.LoginPrompt := False;
    myConn.UserName := 'UserName';
    myConn.Password := 'Password';
    myConn.Server := 'Host';
    myConn.Database := 'DBName';
    myConn.Connected := True;
    myQuery := TMyQuery.Create(nil);
    myQuery.Unidirectional := True;
    myQuery.Options.CreateConnection := False;
    myQuery.Connection := myConn;
    Fstat := None;
    Fstream := TMemoryStream.Create;
  end;
  
  destructor TProcessQuery.Destroy;
  begin
    if Assigned(myConn) then begin
      myConn.Close;
      myConn.Disconnect;
      FreeAndNil(myConn);
    end;
  end;
  
  procedure TProcessQuery.ExecuteQuery;
  begin
    Status := Started;
    myQuery.SQL.Text := '&lt;Some Query&gt;';
    myQuery.Open;
    try
      try
      while not myQuery.Eof do
      begin
           Status := Inprogress;
           //Add to FStream which would be returned to user.
      end;
      except
          on Exception do
          Status := Error;
      end;
    finally
      myQuery.Close;
    end;
  end;
  
  { TSessionData }
  
  constructor TSessionData.Create;
  begin
    FProcessQuery :=  TProcessQuery.Create;
  end;
  
  function TSessionData.GetMemStream: TMemoryStream;
  begin
    result := FProcessQuery.Fstream;
  end;
  
  function TSessionData.GetQueryStat: TStatus;
  begin
    result := FProcessQuery.Status;
  end;
  
  procedure TSessionData.RunLongQuery;
  begin
    FProcessQuery.ExecuteQuery
  end;
  </code></pre>