Jump to content
Ian Branch

Loop a Delay in a thread??

Recommended Posts

Hi Team,

I have ths construct:

    var GeneralThread := TThread.CreateAnonymousThread(procedure
      begin
        //
        tqServerSessions.Close;
        //
        try
          if TEDBEngineSessionManager(DBSWhoIsOn.Handle).TryLock(1) then
          begin
            TEDBEngineSessionManager(DBSWhoIsOn.Handle).Unlock;
            { Session is not locked and is safe for you to use }
            tqServerSessions.ExecSQL;
          end
          else
            // Delay for the specified amount of time
            TThread.Sleep(500);
          //
        except
          ...
          ...
        end;
        //
        tqServerSessions.Last;
        //
        ...
        ...
      end);

How do I safely loop the Try-Else  for x times, and then break out after x times?

Share this post


Link to post
15 hours ago, Ian Branch said:

I have ths construct:

...

How do I safely loop the Try-Else  for x times, and then break out after x times?

Seriously?  Can't you just use a standard For loop?  I don't understand the issue.

Share this post


Link to post

@Ian Branch

 

maybe some like this:  code attached...

procedure TMyThread.Execute;
var
  LWaitRestult: TWaitResult;
begin
  // inherited;
  //
  FTicks64 := GetTickCount64;
  //
  while not Terminated do
    begin
      LogMe(Self.ThreadID.ToString + ',  ...Thread is running ... 1 : Delay: ' + FDelay.ToString);
      //
      LWaitRestult       := FEvent.WaitFor(FDelay);
      FMyEventWaitResult := LWaitRestult;
      //
      if (LWaitRestult = TWaitResult.wrTimeout) then
        begin
          LogMe(Self.ThreadID.ToString + ', hello timer: ' + FDelay.ToString + ', WaitResult: ' + LWaitRestult.ToString);
          //
          FTicks64 := (GetTickCount64 - FTicks64);
          //
          break;
        end;
      //
      LogMe(Self.ThreadID.ToString + ',  ...Thread is running ... 2 : Delay: ' + FDelay.ToString);
    end;
  //
  if (LWaitRestult <> TWaitResult.wrTimeout) then
    begin
      FTicks64           := (GetTickCount64 - FTicks64);
      FMyEventWaitResult := LWaitRestult;
    end;
end;

 

bds_hiMX78Ow7C.gif

 

code.txt

Edited by programmerdelphi2k

Share this post


Link to post

Hi Remy,

Yes, seriously. 

I don't really understand what goes on in Threads and therefore what you can and can't do.

I am still learning about them.

 

p2k,

Tks.  I will have a look.

 

Tks to both of you.

 

Regards,

Ian

Share this post


Link to post

maybe a little fix to get FTicks64 when the end the Thread....

procedure TMyThread.Execute;
 ...
  //
  while not Terminated do
  ...
  end;
  //
  if (LWaitRestult <> TWaitResult.wrTimeout) then // wrSignaled, etc...
    FTicks64 := GetTickCount64 - FTicks64;
end;

image.thumb.png.1a4b9890b3cd999948e2a61b9ab190c2.png

Edited by programmerdelphi2k

Share this post


Link to post
18 hours ago, Ian Branch said:

I don't really understand what goes on in Threads and therefore what you can and can't do.

If you need to loop some code x times and break out on some condition, then the simplest logic would be:

 


for i := 0 to x do
  begin
   if TEDBEngineSessionManager(DBSWhoIsOn.Handle).TryLock(1) then
     begin
       ...
       // if succesfull break out of the loop
       Break;
     end
   else
     TThread.Sleep(500);
  end;

 

 

Share this post


Link to post
1 hour ago, Dalija Prasnikar said:

If you need to loop some code x times and break out on some condition, then the simplest logic would be:

 



for i := 0 to x do
  begin
   if TEDBEngineSessionManager(DBSWhoIsOn.Handle).TryLock(1) then
     begin
       ...
       // if succesfull break out of the loop
       Break;
     end
   else
     TThread.Sleep(500);
  end;

 

 

That will loop the code (x+1) times 🙂

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×