TiComponent.Lock
TiComponent See Also
Used to prevent competing threads from accessing the component at the same
time and causing possible conflicts.
procedure Lock;
Description
Use the Lock method in a multithreaded application to prevent competing
threads from accessing the component at the same time and causing possible conflicts.
Before a thread attempts to call methods or modify properties of the
component, call the Lock method to reserve access to the component. After the thread
completes all method calls and property modifications on the component, call the
Unlock method.
The paint method for the component will automatically call the Lock method
before painting and call the Unlock method after completing the painting. This
will prevent thread conflicts with the main application thread.
The Lock and Unlock method utilize a critical section API object and must be
paired. Each call to Lock increments the Lock count for the thread and each call
to Unlock decrements the Lock count for the thread.
If two threads call the lock method, only one thread will be successful and be
allowed to continue. The other thread will be suspended until the first thread
calls the Unlock method which will result in the second thread resuming.
If you are attempting to lock more than one object at a time, there is the
potential for a deadlock condition to occur. If Thread1 attempts to lock
Component1 first and then Component2, and Thread2 attempts to lock Component2 first and
then Component1, the deadlock occurs if Thread1 is successful in locking
Component1 but before it can lock Component2, Thread2 successfully locks it. Since
both threads need to access Component1 and Component2 and each thread holds one
lock, they will wait for each other forever. To prevent this problem, make sure
the components are locked in the same order to prevent any possible deadlock
conditions.
Example
Delphi
iComponent.Lock;
C++ Builder
iComponent->Lock();
Contents | Index | Previous | Next