Recent

Author Topic: Simultaneous occurrences  (Read 420 times)

Nera

  • Full Member
  • ***
  • Posts: 103
Simultaneous occurrences
« on: July 05, 2020, 01:28:47 pm »
Hi guys

I have a thread (synchronized) and a 10 second timer. The thread writes to a Tedit and the timer takes the Tedit value. It happens that sometimes the reading of Tedit comes with the wrong value. What could be happening?

Thank you

jamie

  • Hero Member
  • *****
  • Posts: 3540
Re: Simultaneous occurrences
« Reply #1 on: July 05, 2020, 02:15:17 pm »
When you say Synchronized I assume you mean you are using a Sync Call in the thread to write to the TEDIT field ?

 So I guess you could use a critical section to ward off multiple accesses to the TEDIT control.

 The timers in Windows comes in two flavors, message or Procedure callback. If the call back version is being used then its possible maybe it is being accessed at the same time as your sync call from the thread..

 Try a criticalSection for that.
The only true wisdom is knowing you know nothing

Nera

  • Full Member
  • ***
  • Posts: 103
Re: Simultaneous occurrences
« Reply #2 on: July 05, 2020, 04:52:06 pm »
" Try a criticalSection for that."

how can I do this?


jamie

  • Hero Member
  • *****
  • Posts: 3540
Re: Simultaneous occurrences
« Reply #3 on: July 05, 2020, 10:54:22 pm »
create variable somewhere that is global to both the thread and the procedure of where the code is taking place and inside the code block you test it.

In global space somewhere

var
 Busy :TCriticalSection;
-- in the Form Create Section---

 Busy := TCriticalSection.Create;

--- in the code block where this needs the be checked --

 Busy.Enter;

 if its not busy then it will continue in your code, if it is busy then it will not return from that call until its not busy
 
 when done executing the sensitive code do this before you exit the body.,.

 Busy.Leave;

that will clear the lock
so you may need to do this with the timer code and the Sync call code from the thread so the two do not collide
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 2920
  • POKE 54296,15
    • Eye-Candy Controls
Re: Simultaneous occurrences
« Reply #4 on: July 05, 2020, 10:59:56 pm »
There is demo: .../lazarus/examples/multithreading/criticalsectionexample1.lpr
Lazarus 2.1.0 r63881 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6628
  • Debugger - SynEdit - and more
    • wiki
Re: Simultaneous occurrences
« Reply #5 on: July 05, 2020, 11:00:52 pm »
If *all* access to TEdit is done in TThread.Synchronize calls, then there should be no need for a critical section.

Synchronize runs the code in the main thread. So all access is from the main thread.

 

TinyPortal © 2005-2018