Creating Run Time Components in Windows Service

Asked

Viewed 49 times

1

I’m trying to make a Windows service in the Delphi 10.2 and in one of the procedure I need to create a TMemo only when I execute this error below.

Project AutoPub.exe raised exception class EInvalidOperation with
message 'Control 'TMemo($043A90C0)' has no parent window. Path:
TServiceApplication($03164E50)
.svcAutoPub <br>
.TMemo($043A90C0)'

I’ll put the part where the error occurs.

procedure TsAutoPub.GetPublicacoes(var Nome: string; Token, Cod: string; Periodo: TDate);
var
  Data: string;
begin
  Data := StringReplace(DateToStr(Date()),'/','-',[rfReplaceAll]);

==> MemoJson := TMemo.Create; // Aqui onde da o erro!!

  GETRequest.Params.Items[0].Value := NomeRelacional;
  GETRequest.Params.Items[1].Value := Token;
  GETRequest.Params.Items[2].Value := CodEscritorio;
  GETRequest.Params.Items[3].Value := FormatDateTime('yyyy-mm-dd', Periodo);
  GETRequest.Params.Items[4].Value := FormatDateTime('yyyy-mm-dd', Periodo);

  try
    try
      GETRequest.Execute;
    finally
      MemoJson.Lines.Clear;
      MemoJson.Lines.Add('{"Publicações":'+#13+GETResponse.JSONText+'}');

      if not DirectoryExists(tbParametosPASTA_BD.AsString+'\Publicações') then
       CreateDir(tbParametosPASTA_BD.AsString+'\Publicações');

      MemoJson.Lines.SaveToFile('C:\Autosad\Banco\Publicações - '+Data+'.json');
    end;
  except on E: ERESTException do
     ShowMessage(IntToStr(GETResponse.StatusCode) + ':' + GETResponse.StatusText);
  end;
end;

I wanted to know how to create this TMemo in the windows service correctly. or where the error is in the code.

  • As stated by Leonardo Getulio, use the TStrings that will serve your purpose.

1 answer

3


A Delphi service cannot create a Tmemo because it is a graphic component and needs a "Parent" designed to draw inside it.

The most correct solution in your case to manipulate these texts would be to use Tstrings or Tstringlist. I particularly prefer Tstringlist. The replacements in the case would look like this:

var
  MemoJson: TStringList; 

//
MemoJson := TStringList.Create();
MemoJson.Clear;
MemoJson.Add('{"Publicações":'+#13+GETResponse.JSONText+'}');
MemoJson.SaveToFile(); 
  • Opa! I’m going to do the tests here... I didn’t get involved in this graphic component issue.

  • It even works more when I write . pdf the text comes out in a row and is horrible. o iria fica como variável nem vai ser draft...

  • I was able to solve it differently.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.