Recent

Author Topic: [Solved] Is it possible to Reset rtleventwaitfor time?  (Read 4769 times)

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
[Solved] Is it possible to Reset rtleventwaitfor time?
« on: May 05, 2015, 11:06:07 pm »
Hi every one.

I am using RTLEventWaitFor(RTLEvent, FTimeOut) to implement a custom timer. I am using the TThread.Synchronize as the OnTimer event, similar to the wiki example. My current Execute procedure is:

Code: [Select]
procedure TMyThread.Execute;
begin
  while not Terminated do
    if Enabled then
      begin
        RTLeventWaitFor(FRTLEvent, FTimeOut);
        Synchronize(@ClockOnTimer);
      end
    else RTLeventWaitFor(FRTLEvent);
end; 
Right now, I am trying to implement a Reset functionality. So, would be possible to reset the rtleventwaitfor time directly after its execution? What do you think about this ideia??

Edit.:

What I was looking for was a way to override the WaitFor TimeOut during the RTLeventWaitFor execution/waiting.

Seems that it is not possible. At least with my current knowledge  :D

See my current solution:


Code: [Select]
procedure TClockThread.Reset;
begin
  if not FMustReset then
  begin
    FMustReset := True;
    RTLeventSetEvent(FRTLEvent);
  end
end;

procedure TClockThread.ClockOnTimer;
begin
  if not Enabled then Exit;
  if FMustReset then
    begin
      FMustReset := False;
      Exit;
    end;
  if Assigned(FOnTimer) then FOnTimer(Self);
end;
« Last Edit: May 06, 2015, 04:57:18 am by carlitus »
Be mindful and excellent with each other.
https://github.com/cpicanco/

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is it possible to Reset rtleventwaitfor time?
« Reply #1 on: May 06, 2015, 01:05:18 am »
There's one already:
http://www.freepascal.org/docs-html/rtl/system/rtleventresetevent.html

I prefer OO style using TEventObject, though. Attached is an example of how to use it (see TJob class).

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Is it possible to Reset rtleventwaitfor time?
« Reply #2 on: May 06, 2015, 01:19:44 am »
Thanks for the hint Leledumbo, I will check it for sure. I was reading the rtleventresetevent docs and they say that "... Resetting an event that is not set (or was already reset) has no effect". Should I assume that RTLeventWaitFor will set the event and that this "set" is not related to the RTLeventSetevent method?
Be mindful and excellent with each other.
https://github.com/cpicanco/

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is it possible to Reset rtleventwaitfor time?
« Reply #3 on: May 06, 2015, 01:27:57 am »
Thanks for the hint Leledumbo, I will check it for sure. I was reading the rtleventresetevent docs and they say that "... Resetting an event that is not set (or was already reset) has no effect". Should I assume that RTLeventWaitFor will set the event and that this "set" is not related to the RTLeventSetevent method?
No, RTLeventSetevent must be called in order for RTLeventWaitFor to notice that the event it set (and thus, release the wait lock). The documentation should be understood as is, or in code language:
Code: [Select]
// The doc says by default, the thread will be suspended indefinitely -> the event is not set (in reset state) by default
RTLeventWaitFor(FRTLevent);
// then somewhere else...
RTLeventResetEvent(FRTLevent);
// will be useless. After all, it's already waiting... adding another call won't make it wait twice or more, there's one and only one state at a time, no way of nesting

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Is it possible to Reset rtleventwaitfor time?
« Reply #4 on: May 06, 2015, 01:39:31 am »
So, it is not possible to reset the time directly, say, from a running (waiting) RTLeventWaitFor through the RTLeventResetEvent. I am considering the RTLeventWaitFor(FRTLEvent, FTimeOut) overloaded method. What I really want is just to reset the TimeOut timer.
« Last Edit: May 06, 2015, 04:57:56 am by carlitus »
Be mindful and excellent with each other.
https://github.com/cpicanco/

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Is it possible to Reset rtleventwaitfor time?
« Reply #5 on: May 06, 2015, 01:45:40 am »
The resulting behavior is an extended timeout.
Be mindful and excellent with each other.
https://github.com/cpicanco/

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Is it possible to Reset rtleventwaitfor time?
« Reply #6 on: May 06, 2015, 04:49:15 am »
I reached the following solution by now. Please, make me know if you have a better idea considering speed and time precision/granularity!

Code: [Select]
procedure TClockThread.Reset;
begin
  if not FMustReset then
  begin
    FMustReset := True;
    RTLeventSetEvent(FRTLEvent);
  end
end;

procedure TClockThread.Clock;
begin
  if not Enabled then Exit;
  if FMustReset then
    begin
      FMustReset := False;
      Exit;
    end;
  if Assigned(FOnTimer) then FOnTimer(Self);
end;
« Last Edit: May 06, 2015, 04:54:03 am by carlitus »
Be mindful and excellent with each other.
https://github.com/cpicanco/

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: [Solved] Is it possible to Reset rtleventwaitfor time?
« Reply #7 on: May 06, 2015, 11:56:31 am »
If you use trunk or fixes_3_0, did you try to use queue() instead of synchronize?

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: [Solved] Is it possible to Reset rtleventwaitfor time?
« Reply #8 on: May 06, 2015, 09:08:04 pm »
If you use trunk or fixes_3_0, did you try to use queue() instead of synchronize?

marcov, please, could you elaborate your question? What is a trunk? What is fixes_3_0?

Edit.: Ok I got it. No I am not using the trunk version neither Lazarus or FPC.

reading this right now: http://forum.lazarus.freepascal.org/index.php?topic=20767.0
« Last Edit: May 07, 2015, 03:57:07 am by carlitus »
Be mindful and excellent with each other.
https://github.com/cpicanco/

 

TinyPortal © 2005-2018