Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Timer ohne Formular (https://www.delphipraxis.net/57686-timer-ohne-formular.html)

meisteralex 25. Nov 2005 08:19


Timer ohne Formular
 
Seruvs hab hier ein Programm geschrieben, welches mit 3 Timern arbeitet.
Habe es mit einem Formular geschreiben, welches ich auf invisible setzt, da das Programm im Hintergrund (unsichtbar) laufen soll.
Nun hab ich mir gedacht ich könnte das Programm ein bischen abspecken, indem ich es ohne die Gui/Winform implementiere.
Wie mache ich das am besten ?
Wie kann ich Timerelemente erzeugen, ohen ein Formular zu benutzen und wie implementiere ich dann die OnTimer-Prozeduren, welche ich sonst durch einen Doppelklick auf das Element bekomme.

Vielen Dank für die Hilfe.

Luckie 25. Nov 2005 08:22

Re: Timer ohne Formular
 
Hier hat es einen beispielcode für einen Timer in er Konsole:
http://www.luckie-online.de/Develope...n_Konsole.html

Hätte sich aber bestimmt auch durch die Suche finden lassen.

meisteralex 27. Nov 2005 00:04

Re: Timer ohne Formular
 
versteh ich nicht ganz, kann mir jemand den code erklären ?

sniper_w 27. Nov 2005 00:13

Re: Timer ohne Formular
 
Auszug aus BallonKiller:
Delphi-Quellcode:
type
  TActorEvent = procedure (Sender:TObject) of object;

  TMyTimer = class
    private
      FOnTimer:TActorEvent;
      FInterval:Cardinal;
      FHandle:Cardinal;
      FOwner:Cardinal;
    protected
      procedure SetInterval(Value:Cardinal);
    public
      constructor Create(AOwner:Cardinal);
      procedure AfterConstruction();override;
      procedure BeforeDestruction();override;
      procedure DoTimerProc();
    published
      property OnTimer:TActorEvent read FOnTimer write FOnTimer;
      property Interval:Cardinal read FInterval write SetInterval;
  end;

[...]
procedure TimerCallBack( Hwnd_ : HWND;   // handle of window for timer messages
                        uMsg :UINT;   // WM_TIMER message
                        idEvent :UINT;   // timer identifier
                        dwTime :DWORD    // current system time
                        );stdcall;
  var CallingTimer:TMyTimer;
begin
  CallingTimer := TMyTimer(idEvent);
  CallingTimer.DoTimerProc();
end;

{ TMyTimer }

procedure TMyTimer.AfterConstruction();
begin
  inherited;
  FInterval := 1000;
  FHandle := SetTimer(FOwner, Cardinal(Self), FInterval,@TimerCallBack);
end;

procedure TMyTimer.BeforeDestruction();
begin
  inherited;
  KillTimer(FOwner,Cardinal(Self));
end;

constructor TMyTimer.Create(AOwner: Cardinal);
begin
  FOwner := AOwner;
end;

procedure TMyTimer.DoTimerProc();
begin
  if Assigned(FOnTimer) then
    FOnTimer(Self);
end;

procedure TMyTimer.SetInterval(Value: Cardinal);
begin
  if FInterval <> Value then
  begin
    FInterval := Value;
    KillTimer(FOwner,Cardinal(Self));
    FHandle := SetTimer(FOwner, Cardinal(Self), FInterval,@TimerCallBack);
  end;
end;
Hier :
Delphi-Quellcode:
constructor TMyTimer.Create(AOwner: Cardinal);
Wenn du kein Fenster hast, kann auch 0 übergeben werden. ( glaube ich )

Luckie 27. Nov 2005 00:36

Re: Timer ohne Formular
 
Zitat:

Zitat von sniper_w
Hier :
Delphi-Quellcode:
constructor TMyTimer.Create(AOwner: Cardinal);
Wenn du kein Fenster hast, kann auch 0 übergeben werden. ( glaube ich )

Und an welches Fenster soll Windows dann die WM_TIMER Nachrichten schicken?

sniper_w 27. Nov 2005 00:38

Re: Timer ohne Formular
 
:shock:
Es gibt aber eine TimerCallback ?! Oder habe ich was falsch verstanden ?

Zitat:

hWnd

Identifies the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.

Luckie 27. Nov 2005 01:08

Re: Timer ohne Formular
 
Zitat:

When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER.
Die Callback wird also ausgeführt, wenn das Fenster eine WM_TIMER Nachricht bekommt. Und wenn du kein fensterhandle angibst, wo soll dann Windows die WM_TIMER Nachricht hinschicken? Also so würde ich das sehen.

sniper_w 27. Nov 2005 01:11

Re: Timer ohne Formular
 
:roll:
Zitat:

An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the DispatchMessage function simply calls the callback function instead of the window procedure.

Luckie 27. Nov 2005 02:40

Re: Timer ohne Formular
 
Richtig. Les dir den letzten Satz noch mal durch:
Zitat:

When you specify a TimerProc callback function, the DispatchMessage function simply calls the callback function instead of the window procedure.
Und jetzt guck dir die Nachrichtenschleife mal an:

Delphi-Quellcode:
while GetMessage(msg,0,0,0) do
  begin
    TranslateMessage(msg);
    DispatchMessage(msg);
  end;
DispatchMessage ruft also die Callback Funktion auf, anstatt die WM_TIMER Nachricht an die Fenster Prozedur weiterzugeben, wo dann normalerweise WM_TIMER abgefagen und darauf reagieren würde. Und bei welcher Nachricht ruft DispatchMessage jetzt die Callback Funktion auf? Richtig, wenn GetMessage eine WM_TIMER Nachricht aus der Nachrichtenschlage für das Fenster abgeholt hat. Und wie soll Windows eine WM_TIMER Nachricht in die richtige Nachrichtenschlage für das richtige Fenster stellen, wenn du null angibst?

sniper_w 27. Nov 2005 09:37

Re: Timer ohne Formular
 
Einfach gesagt, ich habe recht ;).
Delphi-Quellcode:
var
  Form1:TForm1;
  myTimer:TMyTimer;

procedure TForm1.Button1Click(Sender: TObject);
begin
 edit1.Text := DateTimeToStr( Now );
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  myTimer := TMyTimer.Create( 0 );
  myTimer.OnTimer := Button1Click;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  myTimer.Free;
end;
ABER, die TimerCallback muss dann so aussehen ( LEIDER... ):
Delphi-Quellcode:
procedure TimerCallBack( Hwnd_ : HWND;  // handle of window for timer messages
                        uMsg :UINT;  // WM_TIMER message
                        idEvent :UINT;  // timer identifier <- das wird jetzt ignoriert....
                        dwTime :DWORD   // current system time
                        );stdcall;
  //var CallingTimer:TMyTimer;
begin
  //CallingTimer := TMyTimer(idEvent);
  //CallingTimer.DoTimerProc();
  myTimer.DoTimerProc;
end;
und das nur weil nIDEvent Parameter ignoriert wird.
Zitat:

hWnd

Identifies the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:53 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz