Lock Component methods and properties
Use the Lock component to make sure that a section of code does not run multiple times simultaneously.
For example, you can use this component when an automation is triggered by a user clicking a button. If the user double clicks the button, two automations might run at the same time, performing the same steps. To prevent this from happening, use the Lock component. Place the Lock component in a Global container. Make sure that it is a global component.
Here is an example automation that demonstrates the correct way to use the Lock component to prevent an automation from running simultaneously.
If the user double clicks the button, two instances of the automation run.
The first instance of the automation requests a lock and receives it. The system then processes the work required and when finished, it releases the lock.
The second instance either sees that execution is locked and stops running or requests a lock and times out in one second. Either way, the execution of the second instance does not proceed while the first instance is running.
Here is an example automation that demonstrates queuing requests so that they run sequentially.
If the user double clicks the button, two instances of the automation run.
The first instance of the automation requests a lock and receives it. The system then processes the work required and when finished, it releases the lock.
The second instance requests a lock and waits up to 15 seconds before it times out. The system does not run the second instance while it is running the first instance. Instead, the system runs the second instance as soon as the lock is released, if that occurs before the timeout.
The Lock component has the following methods:
Method |
Description |
RequestLock
|
Requests a lock to prevent a section of code from running. Use the timeout parameter to specify the number of seconds that the control waits for a lock before failing. If the RequestLock is successful, you must release it on the same thread when execution finishes. If the lock is not released on the same thread, the lock is not released until you restart the solution. To prevent a perpetual lock, the best practice is to do all the necessary work in another automation and wrap that automation with a Try..Catch. This approach makes sure that the lock is always released. |
ReleaseLock
|
Releases a lock that prevented code from running. After you call the ReleaseLock method, the system process the next request. If there are no pending requests, the next execution trigger can request a lock. |
Use these Lock component properties while processing the request to determine if other requests are pending:
Property |
Description |
IsRequestPending |
Returns a True or False based whether there are requests for a lock pending. |
PendingRequests |
Returns the number of requests. |
IsLocked |
Returns a True or False based on the current condition of the lock. |
Related information
For more information, see Lock Component.
Privacy | Trademarks | Terms of Use | Feedback
Updated: 01 July 2024
© 2016 - 2024 Pegasystems Inc. Cambridge, MA All rights reserved.