Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: Update TaskInWorkerThread.py #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions scripts/TaskInWorkerThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,38 @@ def run(self, ctx):
if not isinstance(ctx, IGraphicalClientContext):
print('This script must be run within a graphical client')
return
# will start a worker thread and run the task; a pop-up will be shown to provide a way to interrupt the task
ctx.executeAsync('Counting...', SimpleTask())
print('Done.')
# will start a worker thread and run the task;
# a pop-up will be shown to provide a way to interrupt the task
try:
ctx.executeAsync('Counting...', SimpleTask())
# under the hood com.pnfsoftware.jeb.util.concurrent.ThreadUtil.start( ) is used to launch the thread

except (InterruptedException,KeyboardInterrupt,):
print('The task was interrupted')

#https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Thread.html#interrupt()
#"Interrupts this thread.
# If none of the previous conditions hold then this thread's interrupt status will be set."
#BUT
# If this thread is blocked in an invocation of the wait() ... join(), ... sleep..., then
# its interrupt status will be cleared !!!
# and it will receive an InterruptedException.

# By this we set the missing interrupted status so SimpleTask gets the signal to stop
SimpleTask.WorkerThread_crutch.interrupt()
else:
print('Done.')

class SimpleTask(Runnable):
def run(self):
SimpleTask.WorkerThread_crutch=Thread.currentThread()

for i in range(10):

# react to user pressing Cancel
if Thread.interrupted():
print('The task was interrupted')
break

print('Counter: %d' % i)
time.sleep(1)