Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti-threading Delphi synchronization using Critical Sections between a Timer and other threads
    primarykey
    data
    text
    <p>It is a problem of synchronization. I made a simplification for my problem so I have a VCL Timer and a few threads. The threads are trying to write things in two bitmaps, and the timer is trying to draw the bitmaps into Images(TImage). Here is a modification of my code to be easier to understand(this paste code is very vague).</p> <pre><code>//------------------------------------------------------------------------------ Procedure TMyForm.Add(iX,iY,iNr:integer); begin EnterCriticalSection(csCriticalSection); bmRed.Canvas.Lock; bmBlue.Canvas.Lock; //.... drawing etc... bmRed.Canvas.TextOut(iX,iY,IntToStr(iNr)); bmBlue.Canvas.TextOut(iX,iY,IntToSTr(iNr)); bmRed.Canvas.Unlock; bmBlue.Canvas.Unlock; LeaveCriticalSection(csCriticalSection); end; //------------------------------------------------------------------------------ procedure TMyForm.tmTimer(...); begin EnterCriticalSection(csCriticalSection); bmRed.Canvas.Lock; bmBlue.Canvas.Lock; //Drawing on bmRed and bmBlue imBlue.Canvas.Draw(bmBlue); imRed.Canvas.Draw(bmBlue); bmRed.Canvas.Unlock; bmBlue.Canvas.Unlock; LeaveCriticalSection(csCriticalSection); end; //------------------------------------------------------------------------------ Procedure TMyThread.Execute(); begin Randomize; while not terminated do begin MyFormInstance.Add(Random(100),Random(100),Random(100)); Sleep(20); end; end; initialization InitializeCriticalSection(csCriticalSection); finalization DeleteCriticalSection(csCriticalSection); </code></pre> <p>Usually the threads gives me errors when entering in the critical section or after a few seconds the timer will no longer draw me anything.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload