# Dynamic and quitting the kernel

No doubt Dynamic is one of the most interesting functions in Mathematica. Not rarely we observe seemingly strange behaviour, as in the following situation.

x=RandomReal[];
Style["Some text", 20, Background->Dynamic[Hue[x]]]
Slider[Dynamic[x]]
Dynamic[x]


The background colour of the text, the position of the slider and the display of Dynamic are all synchronized with the kernel variable x. Move the slider and observe the changes.

Quit the kernel, by evaluating Quit[] or with the evaluation menu. Then there is no kernel variable x any more, so now synchronization is impossible. The display of the slider and the dynamic do not change, but the background of the text disappears.

Now click somewhere on the slider. The slider and the dynamic do not update, but the background of the text changes according to the clicked position of the slider. So not all synchronization has disappeared. What is going on here?

The documentation of Dynamic states that it can be used in three ways: not only directly as an expression to display, but also for example as a coordinate in a graphics primitive or an object such as a slider, or as a setting for an option. In our example we have all three ways.

With respect to quitting the kernel, the last situation turns out to be different from the two others. LinkSnooper shows that we if we evaluate Dynamic[x] or Slider[Dynamic[x]] and then quit the kernel, the kernel does not restart. But when we evaluate Style["Some text", 20, Background->Dynamic[Hue[x]]] and quit the kernel, immediately the kernel is restarted. Let us have a closer look at the LinkSnooper output.

x=RandomReal[];
Style["Some text", 20, Background->Dynamic[Hue[x]]]


LinkSnooper shows that two dynamic objects with numbers 39935 and 40175 are created. Very likely, one of these refers to the displayed StyleBox and the other to the option Background.

Slider[Dynamic[x]]


LinkSnooper shows that a dynamic object with number 40257 is created.

Dynamic[x]


LinkSnooper shows that a dynamic object with number 40337 is created.

InternalGetTrackedSymbols[]
(* Hold[{MakeBoxes,{40337},False},{x,{39935,40175,40257,40337},False}] *)


Therefore, all three (four) dynamic objects will be updated when the value of the kernel variable x changes.

Quit[]


Due to the presence of the first dynamic object, the kernel is closed and immediately a new kernel is started. LinkSnooper does not show any other interaction between the new kernel and the frontend, but during the startup of the new kernel something must have happened:

In[1]:= InternalGetTrackedSymbols[]
Out[1]= Hold[{x,{26831,40175},False}]


It turns out that the object 40175 is the displayed StyleBox. When we closed the old kernel, immediately a new kernel was launched in which the StyleBox was synchronized with the (new) kernel variable x. When we click on the slider, the kernel variable x gets the value of the clicked position of the slider. The kernel variable x is connected to the object 40175, so the StyleBox will be updated, but not connected to the objects 40257 and 40337, so the slider and the dynamic will not be updated.