Ian Branch 127 Posted May 10, 2023 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
Remy Lebeau 1394 Posted May 10, 2023 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
programmerdelphi2k 237 Posted May 10, 2023 (edited) @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; code.txt Edited May 11, 2023 by programmerdelphi2k Share this post Link to post
Ian Branch 127 Posted May 10, 2023 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
programmerdelphi2k 237 Posted May 10, 2023 (edited) 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; Edited May 10, 2023 by programmerdelphi2k Share this post Link to post
Lars Fosdal 1792 Posted May 11, 2023 WaitForMultipleObjects or WaitForSingleObject or the ..Ex versions allows waiting with a minimum of looping, i.e. less CPU usage. Examples of usage can be found f.x. in OmniThreadLibrary. Share this post Link to post
Dalija Prasnikar 1396 Posted May 11, 2023 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
Vandrovnik 214 Posted May 11, 2023 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 🙂 1 Share this post Link to post
Dalija Prasnikar 1396 Posted May 11, 2023 1 hour ago, Vandrovnik said: That will loop the code (x+1) times 🙂 Counting is hard 1 Share this post Link to post