@@ -59,6 +59,10 @@ public class AsyncTask {
5959 * push is used for current task
6060 */
6161 private AtomicInteger missedPolls = new AtomicInteger ();
62+ /**
63+ * {@code true}, if thread may be interrupted if UI/Component detaches
64+ */
65+ private boolean mayInterrupt = true ;
6266
6367 /**
6468 * Create a new task
@@ -78,33 +82,31 @@ public void push(Command command) {
7882 if (parentUI == null ) {
7983 return ;
8084 }
81- if (missedPolls .get () == PUSH_ACTIVE && parentUI .getPushConfiguration ().getPushMode () == PushMode .MANUAL ) {
82- parentUI .accessSynchronously (() -> {
83- try {
84- command .execute ();
85+ boolean mustPush = missedPolls .get () == PUSH_ACTIVE && parentUI .getPushConfiguration ().getPushMode () == PushMode .MANUAL ;
86+ parentUI .accessSynchronously (() -> {
87+ try {
88+ command .execute ();
89+ if (mustPush ) {
8590 parentUI .push ();
86- } catch (UIDetachedException ignore ) {
87- // Do not report
88- // How could this even happen?
89- } catch (Exception e ) {
90- // Dump
91- asyncManager .handleException (this , e );
9291 }
93- });
94- } else {
95- // Automatic -- changes will be pushed automatically
96- // Disabled -- we're using polling and this is called
97- // within UIDLRequestHandler
98- parentUI .accessSynchronously (command );
99- }
92+ } catch (UIDetachedException ignore ) {
93+ // Do not report
94+ // How could this even happen?
95+ } catch (Exception e ) {
96+ // Dump
97+ asyncManager .handleException (this , e );
98+ }
99+ });
100100 }
101101
102102 /**
103- * Cancel and unregister the task
103+ * Cancel and unregister the task. Thread interruption behaviour is controlled
104+ * by {@link AsyncTask#allowThreadInterrupt()} and
105+ * {@link AsyncTask#preventThreadInterrupt()} methods.
104106 */
105107 public void cancel () {
106108 if (!task .isCancelled () && !task .isDone ()) {
107- task .cancel (true );
109+ task .cancel (mayInterrupt );
108110 }
109111 remove ();
110112 }
@@ -129,6 +131,20 @@ public void await() throws ExecutionException, InterruptedException {
129131 task .get ();
130132 }
131133
134+ /**
135+ * Allow worker thread to be interrupted when UI or Component detaches. Default behaviour.
136+ */
137+ public void allowThreadInterrupt () {
138+ this .mayInterrupt = true ;
139+ }
140+
141+ /**
142+ * Prevent worker thread interruption when UI or Component detaches.
143+ */
144+ public void preventThreadInterrupt () {
145+ this .mayInterrupt = false ;
146+ }
147+
132148 //--- Implementation
133149
134150 /**
0 commit comments