* * *

Recent Posts

Pages: [1] 2 3 ... 10
1
Databases / Re: Lazarus Firebird remote connection problems
« Last post by Ferke on Today at 03:52:25 pm »
The client intall did not help...  %)
2
General / Re: widechar evalution question
« Last post by ASerge on Today at 03:52:22 pm »
Code: Pascal  [Select]
  1. program Project1;
  2. {$APPTYPE CONSOLE}
  3. {$MODE OBJFPC}
  4.  
  5. uses SysUtils;
  6.  
  7. begin
  8.   Writeln('Does the set ":".."@" include');
  9.   Writeln('Halfwidth black square - ', BoolToStr(CharInSet(#$FFFD, [':'..'@']), 'Yes', 'No'));
  10.   Writeln('Question mark - ', BoolToStr(CharInSet('?', [':'..'@']), 'Yes', 'No'));
  11.   Readln;
  12. end.
3
General / Re: Thread Safety?
« Last post by ASerge on Today at 03:41:12 pm »
Made some changes to my last example:
1. As noted by @rvk, threads should not be terminated inside their events. So changed FreeAndNil with FThread := nil, and added FreeOnTerminate := True.
2. The procedure Sleep allows system to switch the threads. A more realistic situation is when the thread itself performs continuous work. Replaced with SpinWait. And Max increased, otherwise it's too fast.
3. If the main thread uses shared variables, then inside the TestThread we can skip changes them right now. It is possible next time, all the same already became not actual. Therefore, not unconditional lock but just an attempt. Because IReadWriteSync does not contain the required function, it is replaced by the use of TRTLCriticalSection (TryEnterCriticalSection).
4. To reduce the time for locking the shared variables inside OnTimer, copy them to local ones, and then apply them to the controls.
5. As noted by @engkin, "Calling Terminate and WaitFor is redundant as TThread.Free leads to calling both", so replace it with FThread.Free (this works for nil as well).
6. Set Timer interval to 200ms. It's pretty fast from the human point of view, but very rarely, from the processor's point of view. In addition eliminated situation, when common variables are requested in OnTimer more often than they change in the TestThread, otherwise the TryEnterCriticalSection call can never be executed.
7. And in the end, microoptimization, due to the replacement of Format with FmtStr, eliminates the try finally block for the temporary string variable.
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, ComCtrls, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.   TTestThread = class(TThread)
  12.   private
  13.     FInfo: string;
  14.     FMax: Integer;
  15.     FProgress: Integer;
  16.     FCriticalSection: TRTLCriticalSection;
  17.   protected
  18.     procedure Execute; override;
  19.   public
  20.     constructor Create(AMax: Integer; ACriticalSection: TRTLCriticalSection);
  21.     property Progress: Integer read FProgress;
  22.     property Info: string read FInfo;
  23.   end;
  24.  
  25.   TForm1 = class(TForm)
  26.     Label1: TLabel;
  27.     ProgressBar1: TProgressBar;
  28.     Timer1: TTimer;
  29.     procedure FormCreate(Sender: TObject);
  30.     procedure FormDestroy(Sender: TObject);
  31.     procedure Timer1Timer(Sender: TObject);
  32.   private
  33.     FCriticalSection: TRTLCriticalSection;
  34.     FThread: TTestThread;
  35.     procedure ThreadTerminated(Sender: TObject);
  36.   end;
  37.  
  38. var
  39.   Form1: TForm1;
  40.  
  41. implementation
  42.  
  43. {$R *.lfm}
  44.  
  45. constructor TTestThread.Create(AMax: Integer; ACriticalSection: TRTLCriticalSection);
  46. begin
  47.   inherited Create(False);
  48.   FMax := AMax;
  49.   FCriticalSection := ACriticalSection;
  50. end;
  51.  
  52. procedure TTestThread.Execute;
  53. begin
  54.   while not Terminated and (FProgress <= FMax) do
  55.   begin
  56.     SpinWait(7000); // Hide some real work
  57.     if TryEnterCriticalsection(FCriticalSection) <> 0 then
  58.     try
  59.       Inc(FProgress);
  60.       FmtStr(FInfo, 'Pos: %d, Total: %.1f%%', [FProgress, FProgress * 100/ FMax]);
  61.     finally
  62.       LeaveCriticalsection(FCriticalSection);
  63.     end;
  64.   end;
  65. end;
  66.  
  67. procedure TForm1.ThreadTerminated(Sender: TObject);
  68. begin
  69.   FThread := nil;
  70.   ProgressBar1.Position := ProgressBar1.Max;
  71.   Label1.Caption := 'Complete!';
  72. end;
  73.  
  74. procedure TForm1.Timer1Timer(Sender: TObject);
  75. var
  76.   LPosition: Integer;
  77.   LCaption: string;
  78. begin
  79.   if Assigned(FThread) then
  80.   begin
  81.     EnterCriticalsection(FCriticalSection);
  82.     try
  83.       LPosition := FThread.Progress;
  84.       LCaption := FThread.Info;
  85.     finally
  86.       LeaveCriticalsection(FCriticalSection);
  87.     end;
  88.     ProgressBar1.Position := LPosition;
  89.     Label1.Caption := LCaption;
  90.   end
  91.   else
  92.     Timer1.Enabled := False;
  93. end;
  94.  
  95. procedure TForm1.FormCreate(Sender: TObject);
  96. begin
  97.   ProgressBar1.Max := 200000;
  98.   Timer1.Interval := 200;
  99.   InitCriticalSection(FCriticalSection);
  100.   FThread := TTestThread.Create(ProgressBar1.Max, FCriticalSection);
  101.   FThread.FreeOnTerminate := True;
  102.   FThread.OnTerminate := @ThreadTerminated;
  103. end;
  104.  
  105. procedure TForm1.FormDestroy(Sender: TObject);
  106. begin
  107.   FThread.Free;
  108.   DoneCriticalsection(FCriticalSection);
  109. end;
  110.  
  111. end.

Please check in Linux.

In my opinion, this is a handy scenario when the work thread performs a lot of work, with almost no delays, while recording the current state in shared variables as quickly as it can. And the main thread displays the state of this data, but in order not to load the processor, it does it slowly (with the help of a timer), but visually fast enough.
4
General / unclear (Duplicate identifier) error
« Last post by fcu on Today at 03:21:55 pm »
Hi
try to compile this with fpc 3.0.4 & fpc 3.1.1 , gives this error (Duplicate identifier "tNode$1$crc7ED07D64")
Code: Pascal  [Select]
  1. {$mode delphi}
  2. type
  3.         tNode<T> = record
  4.           data : T;
  5.           next : ^tNode<T>;
  6.         end;
  7.  
  8.         tLList<T> = object
  9.           procedure add(const item : T);
  10.         private
  11.           head : ^tNode<T>;
  12.         end;
  13.  
  14. procedure tLList<T>.add(const item : T);
  15. var     tmp : ^tNode<T>; // Error: Duplicate identifier "tNode$1$crc7ED07D64"
  16. begin
  17.         //
  18. end;
  19.  
  20. begin
  21. end.
  22.  

am i doing something wrong or its fpc bug ?
5
General / Re: Progress Bar when Main Form Creation
« Last post by howardpc on Today at 03:19:26 pm »
See the attached project for a working skeleton app with a progressbar splash screen.
6
General / Re: Anyone have cross platform enhanced hint function?
« Last post by josh on Today at 02:49:27 pm »
@WP
Superb :)

Does all I need it to do.

I have been playing around and searching for examples; I was getting somewhere but not got to the stage you got at.

Thanks again..
7
General / Re: Anyone have cross platform enhanced hint function?
« Last post by wp on Today at 02:09:59 pm »
Yes that's more complicated because the HTMLPanel does not know how to find the image. The new demo creates a DataProvider for the images.
8
Other / Re: [solved] FPGui Winhandle
« Last post by relocate on Today at 01:59:32 pm »
Hi,

everything is fine now.
I've tested it with the Windows Messagebox. The first time I've tried it, the window hasn't been created, although I thought it has been.
And the second time, the window allready has been destroyed, although I thought it hasn't.
I now called another procedure during the running program and it showed me the widget windowhandle I'd expected. And even createwindow works fine now.
I use it, until I probably made up a own widget for fpgui.

Greetings relocate
9
General / Re: Progress Bar when Main Form Creation
« Last post by WooBean on Today at 01:46:04 pm »
Code: Pascal  [Select]
  1.  
  2. Application.Initialize;  
  3. with TFrmPrgBar.Create(nil) do
  4.         try
  5.         Show;
  6.         Application.ShowMainForm:= True; //next created Form will be the Main Form
  7.         Application.CreateForm(TFrmMain, FrmMain);
  8.         finally
  9.         Free;
  10.     end;
  11.         Application.Run;  

But I get a error message when application.run:

In this line of control.inc an AccessViolation:

Code: Pascal  [Select]
  1.  GetTextMethod := TMethod(@Self.GetTextBuf);

Well, I guess that inside FrmMain.OnCreate event you (want to) call TFrmPrgBar type form but there is no way of addressing it - no variable contains its address as it was hidden by "with TFrmPrgBar.Create(nil) do".
My advice: simplier or working code = better than just invented. Use "with .. do" very very carefully. 
10
General / Re: Error in conditionals - Lazarus 1.8.0 Win32
« Last post by PsaCrypt on Today at 01:40:23 pm »
Thank you Rvk, Integer(Variant) solves the problem.  Still need to learn many of Pascal language (I love xBase).  In first instance I need to convert all my functions, after I think in other resources.
Pages: [1] 2 3 ... 10

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus