I was / am aware about this solution, however, I am still looking for a solution for SetTimer.
Assuming
ShowModal() is processing Win32 messages correctly, then I would guess that the 2nd Form's
HWND is likely being recreated by
ShowModal() after the
OnCreate event has exited, thus losing your timer. In which case, you can work around that by overriding the virtual
CreateWnd() method instead to call
SetTimer().
unit unit2;
{$ifdef fpc}
{$mode objfpc}{$H+}
{$endif}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Windows;
type
{ TFrm2 }
TFrm2 = class(TForm)
mTest: TMemo;
private
intTimerId: integer;
public
procedure WndProc(var Msg: TMessage); override;
procedure CreateWnd; override;
end;
implementation
{$R *.lfm}
const
ctTimerId = 1;
{ TFrm2 }
procedure TFrm2.CreateWnd;
begin
inherited;
intTimerId := SetTimer(Handle, ctTimerId, 1000, nil);
end;
procedure TFrm2.WndProc(var Msg: TMessage);
begin
if Msg.Msg = WM_TIMER then
begin
mTest.Lines.Add('timer');
end
else
begin
inherited WndProc(Msg);
end;
end;
end.
Otherwise, you should create your own hidden
HWND using the Win32
CreateWindow/Ex() function directly just for the timer to use (that is what
TTimer does internally). Then you are not tied to the lifetime of the Form's
HWND.
Of course, it would be easier to just use
TTimer instead.
My application also uses messages for serial communication (no component) and for Ethernet working with winsock directly. I am checking to see if those messages are passing through.
They should, if
ShowModal() is running a proper Win32 message loop. Unless you are tying those operations to the Form's
HWND and it gets recreated after you setup the operations. If there is one thing the VCL has taught me, and this probably applies to the LCL too, is that a Form's
HWND is reliable for sending messages to at any given moment, but it is not reliable for persistent registrations over time, unless you override the
CreateWnd()/
DestroyWnd() methods to update those registrations every time the
HWND is recreated by the framework, for whatever reason.