... | ... | @@ -318,7 +318,35 @@ public class JavaStop { |
|
|
|
|
|
This example also uses an *AtomicBoolean* variable to stop the while loop.
|
|
|
|
|
|
An example in Python:
|
|
|
```python
|
|
|
import cameopy
|
|
|
import sys
|
|
|
import time
|
|
|
from threading import Event
|
|
|
|
|
|
// Init the app.
|
|
|
cameopy.This.init(sys.argv)
|
|
|
|
|
|
// Shared variable.
|
|
|
stopping = Event()
|
|
|
|
|
|
// Define the stop handler.
|
|
|
def stop():
|
|
|
print("Stop handler executed")
|
|
|
stopping.set()
|
|
|
|
|
|
// Register the handler.
|
|
|
cameopy.This.handleStop(stop)
|
|
|
|
|
|
// Loop until stop is triggered.
|
|
|
while not stopping.is_set():
|
|
|
time.sleep(0.1)
|
|
|
```
|
|
|
|
|
|
The Python example follows the same principle, and this time we use the *Event* object provided by the *threading* module to share the stopping state.
|
|
|
|
|
|
WARNING: USE STOP HANDLER FOR canceling blocking calls, etc. |
|
|
To finish, it is highly recommended to implement stop handler this way:
|
|
|
* Do not implement cleaning code e.g. resource deallocation in stop handlers.
|
|
|
* Only write code that unblocks blocking calls: graphical loop, etc.
|
|
|
* Let the cleaning code at the very end of the main. |