CriticalSection example

Hi guys!

I can’t find an example of CriticalSection.

Searching in the forum also didn’t give me a complete picture.

I want to lock updating and removing entries from an array in a multithreading app (no GUI).

Help is appreciated!

This is like a semaphore but there is only one “thing” that the CriticalSection protects and not an array. Adapt the example at .

I think it works.

I’ve added a CriticalSection class property to the thread that does the updating, initialized it in the Run event handler and added Enter & Leave around the ‘critical’ code inside one of its methods.

No errors = Good job?

Good job. Forgot something: you need to ignore IllegalLockingException which can occur when you want to “kill” something. I’m doing this:

#pragma BreakOnExceptions false try if theSemaphore <> Nil then theSemaphore.Release catch err as IllegalLockingException 'just ignore end try #pragma BreakOnExceptions True


I don’t think I need that though.