OmniThreadLibrary forum
News: SMF - Just Installed!
 
*
Welcome, Guest. Please login or register. May 17, 2012, 06:14:15 PM


Login with username, password and session length


Pages: [1]   Go Down

Author Topic: Controlling the time interval of TTimedTask?  (Read 224 times)

Unspoken

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Controlling the time interval of TTimedTask?
« on: February 02, 2012, 02:49:57 AM »

Hi Primoz.

I have a task which runs in background every two seconds. It is created like this (you gave me this example):

Code: [Select]
var
  FTask: IOmniTaskControl

  FTask := CreateTask(TTimedTask.Create())
    .SetTimer(1, 2*1000, @TTimedTask.PeriodicLog)
    .Run;

My question is, can I change the time interval after the task was created. If yes, then how?


Thanks.
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #1 on: February 02, 2012, 03:08:48 AM »

Yes, inside TTimedTask you can call Task.SetTimer(1, ....).
Logged

Unspoken

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #2 on: February 02, 2012, 05:17:53 AM »

Yes, inside TTimedTask you can call Task.SetTimer(1, ....).

How?

FTask.SetTimer() does not seem to have any effect.

So I guess I need to set it up directly on TTimedTask? But how to get to the instance of TTimedTask from FTask?

Is it possible?
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #3 on: February 02, 2012, 05:26:05 AM »

You can store TTimedTask into a separate field.

FTimedTask := TTimedTask.Create;
FTask := CreateTask(FTimeTask);

But most probably you're doing something wrong. Can you show the relevant pieces of code implementing the timer functionality?
Logged

Unspoken

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #4 on: February 02, 2012, 06:03:25 AM »

Yes, the thing is that I would like to avoid storing it in separate field, just to increase readability. However even setting FTimedTask does not work me.

I've attached the example with source code.

Thanks for helping me out:)

Code: [Select]
unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OtlTask, OtlTaskControl, Spin;

type
  TfrmPeriodicTask = class(TForm)
    btnStartPeriodicTask: TButton;
    seSeconds: TSpinEdit;
    lblSecondsInterval: TLabel;
    procedure btnStartPeriodicTaskClick(Sender: TObject);
    procedure seSecondsChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FTask: IOmniTaskControl;
    FTimedTask: TOmniWorker;
    FSeconds: Cardinal;
  public
    { Public declarations }
  end;

type
  TTimedTask = class(TOmniWorker)
  public
    procedure PeriodicLog;
  end;

var
  frmPeriodicTask: TfrmPeriodicTask;

implementation

{$R *.dfm}

procedure TfrmPeriodicTask.btnStartPeriodicTaskClick(Sender: TObject);
begin
  FTimedTask := TTimedTask.Create();
  FTask := CreateTask(FTimedTask)
    .SetTimer(1, FSeconds*1000, @TTimedTask.PeriodicLog)
    .Run;
end;

{ TTimedTask }

procedure TTimedTask.PeriodicLog;
  procedure WriteToFile(const LogFilePath, LogMessage: String);
  var
    LogFile: TextFile;
  begin
    {$i-}
    try
      ForceDirectories(ExtractFileDir(LogFilePath));
      AssignFile(LogFile,LogFilePath);
      Append(LogFile);

      if IOresult <> 0 then
        Rewrite(LogFile);

      Writeln(LogFile, LogMessage);
    finally
      CloseFile(LogFile);
    end;
    {$i+}
  end;
begin
  WriteToFile(ExtractFilePath(ParamStr(0)) + 'LogFile.txt', DateTimeToStr(Now));
end;

procedure TfrmPeriodicTask.FormCreate(Sender: TObject);
begin
  FSeconds := 1;
end;

procedure TfrmPeriodicTask.seSecondsChange(Sender: TObject);
begin
  FSeconds := seSeconds.Value;
  FTimedTask.Task.SetTimer(FSeconds * 1000); //does not work
  //FTask.SetTimer(FSeconds * 1000); //does not work
end;

end.
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #5 on: February 02, 2012, 03:46:30 PM »

The correct way to reset a timer is

Code: [Select]
procedure TfrmPeriodicTask.seSecondsChange(Sender: TObject);
begin
  FSeconds := seSeconds.Value;
  FTask.SetTimer(1, FSeconds*1000, @TTimedTask.PeriodicLog)
end;

Your code resets timer 0, which is the 'default timer'.

But event if you fix that, the program still doesn't work because you have found a bug! I'll fix it today or tomorrow.
Logged

Unspoken

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #6 on: February 02, 2012, 04:02:00 PM »

The correct way to reset a timer is

Code: [Select]
procedure TfrmPeriodicTask.seSecondsChange(Sender: TObject);
begin
  FSeconds := seSeconds.Value;
  FTask.SetTimer(1, FSeconds*1000, @TTimedTask.PeriodicLog)
end;

Your code resets timer 0, which is the 'default timer'.

But event if you fix that, the program still doesn't work because you have found a bug! I'll fix it today or tomorrow.

Glad that I could help you out:)

The question is, how should I know which timer to set up?;) If I will have more timedtasks? There should be some way to get the timer ID, don't you think?
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #7 on: February 02, 2012, 04:15:35 PM »

The bug is squashed in the SVN. Do the Update.

As for your question, you can declare timer procedure as

procedure TTimedTask.PeriodicLog(const timerID: TOmniValue);

Then it will know its ID.

(If that's what you were asking.)

If that was not your question then the answer is that timer IDs are your problems - you have to manage them. After all, it is your code that passes timer ID to the SetTimer call.

Logged

Unspoken

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #8 on: February 03, 2012, 02:17:05 AM »

I think I do not understand, because I do not know what is going on under the hood of OTL.
When I call SetTimer and I give some id for the Timer, than if such ID does not exists in your internal timer list, a new timer is created and associated with the ID?

Edited:

And thank you for solivng the bug, it works as expected now:)
« Last Edit: February 03, 2012, 02:31:03 AM by Unspoken »
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #9 on: February 03, 2012, 02:29:06 AM »

First you called SetTimer(1, interval, event) while creating the task. This created new timer with ID 1 and associated it with an interval and timer event.

When you call IOmniTaskControl.SetTimer(1, interval, event) at some later time (like in your OnChange handler), OTL will just set information for timer 1 to new values.

You can (for example) also call SetTimer(2, interval, event) to set another timer. (Again you can do this while creating a task or at a later time.)

Timers are bound to tasks so you can have two tasks with timer 1 and those will be two totally independant timers.
Logged

Unspoken

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Controlling the time interval of TTimedTask?
« Reply #10 on: February 03, 2012, 02:31:51 AM »

Thanks, it is clear for me now:)
Logged
Pages: [1]   Go Up
 
 

Powered by MySQL Powered by PHP Powered by SMF 2.0.2 | SMF © 2006-2009, Simple Machines LLC

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM