##What is going on
By default the Dynamic
, EventHandlers
and various controllers like Button
and Slider
use a PreemptiveLink to communicate with the kernel.
Synchronous versus Asynchronous Dynamic Evaluations about the PreemptiveLink:
[...] There is no queue; instead, the front end sends one evaluation at a time and waits for the result before continuing with its other work, [...] the front end is completely locked up, and no typing or other actions are possible. [...] (* so Dynamics' updates aswell *)
In your case display = "Correct"; Pause[1]; display = rand
is that "one evaluation" during which the front end is locked up. Therefore there is no reaction for display's
change.
##Solutions
- The shortest one would be to use a MainLink (Asynchronous/Queued).
Unfortunately for Notebook/Cell/FrontEndEventActions
we do not have Method->"Queued"
like in Button
or ActionMenu
.
You can check that this indeed works:
Dynamic[x]
(*check with and without that method*)
Button["test", x = 1; Pause[1]; x = 2, Method -> "Queued" ]
FinishDynamic[]
We can use FinishDynamic[]
to tell the front end that it should (blindly) update all out of date Dynamics
.
This works well:
display = "Correct"; FinishDynamic[]; Pause[1]; display = rand
The Kernel waits till the front end confirms FinishDynamic[]
end, and then proceeds with Pause.
Quite often it is convenient, however, one should keep in mind that this function is going to prompt all dynamics checks.
related: How to catch the “Preemptive” evaluations from within the main loop?How to catch the “Preemptive” evaluations from within the main loop?
ScheduledTasks
Scheduling a procedure is done immediately so it doesn't block the link anymore.
Pause[1]; expr;
can be translated to RunScheduledTasks[expr, {1}]
Nesting and synchronizing multiple tasks may be tricky but here it works as intended.
This automatically gives us:
display = "Correct"; RunScheduledTask[display = rand, {1}]
###Further readings
Evaluation order of Button[]'s actionEvaluation order of Button[]'s action
Working with ScheduledTasks inside DynamicModulesWorking with ScheduledTasks inside DynamicModules