Delphi Can
timer kullanimi hakkinda - Baskı Önizleme

+- Delphi Can (https://www.delphican.com)
+-- Forum: Delphi (https://www.delphican.com/forumdisplay.php?fid=3)
+--- Forum: Genel Programlama (https://www.delphican.com/forumdisplay.php?fid=6)
+--- Konu Başlığı: timer kullanimi hakkinda (/showthread.php?tid=7694)

Sayfalar: 1 2


Cvp: timer kullanimi hakkinda - Tuğrul HELVACI - 27-04-2024

Aşağıda sizin için basit bir timer thread oluşturdum. Her 1 dakikada bir TimeOut isimli metod çağrılıyor. Aynı zamanda bir button daha ekledim ve o buttonun içine de 3 dakikalık ana thread'i bloklayan bir kod yazdım. Ana thread'iniz bloklu durumda iken dahi, yazdığımız thread TimeOut metodunu ana thread'in kuyruğuna bırakmaya devam eder. Yani zaman kaçırmazsınız. Ancak, ana thread'iniz ne zaman bloktan kurtulur ise TimeOut metodlarınız sırası ile hemen işletilir.

Denemek sizden Wink

unit Unit1;

interface

uses
 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
 TForm1 = class(TForm)
   btnStart: TButton;
   btnStop: TButton;
   Memo1: TMemo;
   btnBlockMainThread: TButton;
   procedure btnStartClick(Sender: TObject);
   procedure btnStopClick(Sender: TObject);
   procedure FormClose(Sender: TObject; var Action: TCloseAction);
   procedure btnBlockMainThreadClick(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;
 fTimerThread : TThread = nil;

implementation

{$R *.dfm}

procedure InternalAPCProc(Param : NativeUInt); stdcall;
begin
 //
end;

procedure TimeOut;
begin
 form1.Memo1.Lines.Add(TimeToStr(Time));
end;

procedure StopThread;
begin
 if Assigned(fTimerThread) then
 begin
   QueueUserAPC(@InternalAPCProc, fTimerThread.Handle, 0);

   fTimerThread.Terminate;
   fTimerThread.WaitFor;

   FreeAndNil(fTimerThread);
 end; // if Assigned(fTimerThread) then
end;

procedure StartThread;
begin
 StopThread;

 fTimerThread := TThread.CreateAnonymousThread
                 (
                   procedure
                   var
                     AResult : Cardinal;
                   begin
                     TThread.CurrentThread.FreeOnTerminate := false;

                     while not TThread.CurrentThread.CheckTerminated do
                     begin
                       if SleepEx(1 * 60 * 1000, true) = WAIT_IO_COMPLETION then // 1 dakikada bir kontrol edelim
                         Exit;

                       TThread.Queue(
                         nil,

                         procedure
                         begin
                           TimeOut;
                         end
                       );
                     end; // while not TThread.CurrentThread.CheckTerminated do
                   end
               );

 fTimerThread.Start;
end;

procedure TForm1.btnBlockMainThreadClick(Sender: TObject);
begin
 Sleep(3 * 60 * 1000);
end;

procedure TForm1.btnStartClick(Sender: TObject);
begin
 StartThread;
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
 StopThread;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 StopThread;
end;

end.



timer kullanimi hakkinda - sadikacar60 - 28-04-2024

tesekkurler hocam en ince detayina kadar inceleyecegim.
ihtiyacimi
snc:=MinutesBetween(_OacTime,nw);

seklinde hallettim ama formda bir timer kullanmadan kodla timer kullanmayi sayenizde ogrenecegim insallah
saygilar.