Recent

Author Topic: Wait for threads in console application  (Read 1660 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Wait for threads in console application
« on: June 24, 2018, 12:24:17 pm »
hi All,
what is the best way to wait for threads in a console application?
i am using a _hasRun global variable, but is this the best option?

thank you

Code: Pascal  [Select][+][-]
  1. program gogs_console;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses {$IFDEF UNIX}
  6.   cthreads, {$ENDIF}
  7.   Classes,
  8.   SysUtils,
  9.   unidac10,
  10.   pgprovider10,
  11.   CustApp,
  12.   postgres_connect,
  13.   get_repos_urls;
  14.  
  15. type
  16.   TGogsConsole = class(TCustomApplication)
  17.   protected
  18.     _hasRun: boolean;
  19.     procedure DoRun; override;
  20.  
  21.   public
  22.     constructor Create(TheOwner: TComponent); override;
  23.     destructor Destroy; override;
  24.  
  25.     procedure CatchRepoURLs(ARepoList: array of string);
  26.   end;
  27.  
  28.   procedure TGogsConsole.DoRun;
  29.   var
  30.     c: TPostgresConnInfo;
  31.     h: TCreateDBHandler;
  32.     r: TGogsRepos;
  33.  
  34.   begin
  35.     if not _hasRun then
  36.     begin
  37.       c.ServerIP := '192.168.1.46';
  38.       c.ListenPort := 5432;
  39.       c.Username := 'gogs';
  40.       c.Password := 'gogs';
  41.       c.Database := 'gogs';
  42.  
  43.       h := TCreateDBHandler.Create(c);
  44.       writeln('connected:' + BoolToStr(h.IsConnected, True));
  45.  
  46.       r := TGogsRepos.Create(h.DBConnection, h.DBTransaction);
  47.       r.OnPassResults := @CatchRepoURLs;
  48.  
  49.       WriteLn('xxx');
  50.  
  51.       _hasRun := True;
  52.     end;
  53.   end;
  54.  
  55.   constructor TGogsConsole.Create(TheOwner: TComponent);
  56.   begin
  57.     _hasRun := False;
  58.  
  59.     inherited Create(TheOwner);
  60.     StopOnException := True;
  61.   end;
  62.  
  63.   procedure TGogsConsole.CatchRepoURLs(ARepoList: array of string);
  64.   var
  65.     i: word;
  66.  
  67.   begin
  68.     for i := Low(ARepoList) to High(ARepoList) do
  69.     begin
  70.       WriteLn('TGogsConsole.CatchRepoURLs:' + ARepoList[i]);
  71.     end;
  72.  
  73.     Terminate;
  74.   end;
  75.  
  76.   destructor TGogsConsole.Destroy;
  77.   begin
  78.     inherited Destroy;
  79.   end;
  80.  
  81. var
  82.   Application: TGogsConsole;
  83.  
  84. begin
  85.   Application := TGogsConsole.Create(nil);
  86.   Application.Title := 'GOGS Console';
  87.   Application.Run;
  88.   Application.Free;
  89. end.

(Lazarus 1.8.4 64b, Linux MINT)
Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018