Does there exist a technique to construct a Cell that allows you to both auto update a Dynamic variable and allows you select the code while the variable is being updated?

For example

enter image description here

Generated from the following code

a = 0;
task = CreateScheduledTask[
   a = a + 1
   , 1];
Print@Button["RemoveScheduleTask", RemoveScheduledTask[task]];
InputField[Dynamic[a], Number]

Now if I select 100 in the InputField my selection gets unhighlighted as soon as the variable gets updated, meaning I don't get enough time to edit the variable.

Now if I select the 100 in the second Cell and attempt to change the value nothing changes because of the nature of how Mathematica works.

The data I am working with is multiline so I would prefer if there was some way to modify the Cell output so that I could update the variable while the scheduled task runs in the background. The above is a simplified issue of the issue, but illustrates the functionality quiet clearly.

Question: How do I update a varaible's value if is constantly being updated?

  • $\begingroup$ Partial answer: When you start editing proper -- click in the cell, and type a number or hit delete -- dynamic updating stops and you can edit. Selecting all or part of the number is not considered editing in this case. $\endgroup$
    – Michael E2
    Jul 27, 2013 at 19:01
  • $\begingroup$ @MichaelE2 That works quiet well for the InputField as long as the UpdateInterval is not shorter then 1 second or so. In my real world code the UpdateInterval is variable and not controlled by the user. As a side comment, I'm a little more interested in making the Dynamic Cell update the variable similar to the InputField. Ultimately I might end up opening up a separate question describing how I think a Dynamic Cell should really work, but I would like to see if someone comes up with a working solution. $\endgroup$
    – William
    Jul 27, 2013 at 19:23
  • $\begingroup$ What I described seems to happen even if the update interval is as short as 0.001 sec., but I understand (and understood) that it's not exactly what you're hoping for. As yet, I haven't thought of a way to carry it further. $\endgroup$
    – Michael E2
    Jul 27, 2013 at 21:05
  • $\begingroup$ It seems to me that your approach is asking for trouble, such as unreliability and instability because it introduces race conditions. Why don't you approach your task differently? Consider introducing a second button that pauses the background evaluation, brings up dialog that lets the user enter a value, and then restarts the paused task. $\endgroup$
    – m_goldberg
    Jul 28, 2013 at 0:56
  • $\begingroup$ @m_goldberg That's fair lets assume the data only gets updated on a set interval. Ultimately the the data that is being updated is ideally going to contain actual code for example Graphics[Circle[{0, 0}], ImageSize -> {100, Automatic}] not really sure if context helps $\endgroup$
    – William
    Jul 28, 2013 at 1:27

1 Answer 1


I've done some work on this problem. What I've done addresses the issues raised by the toy example presented in the question. Whether or not it can contribute to the OP's real problem is something only he can decide.

Evaluating update brings up a input dialog which will accept a number from the user. It rounds the number to an integer and then binds the global variable count to the integer.

update[] := (
  count = 
    DialogInput[{val = count},
        Row[{Style["Enter an integer", "Helvetica", 12]}],
        InputField[Dynamic@val, Number],
        Button["OK", DialogReturn[Round@val]]


The following code block does the following:

  • initializes count

  • starts a task which incements count in the background

  • creates two buttons: one to bring up the input dialog; a second to stop and destroy the background task


count = 0;
StartScheduledTask[CreateScheduledTask[++count, 1]];
  Button["Update count", update[], Method -> "Queued"],
  Button["Remove Task", RemoveScheduledTask[First@ScheduledTasks[]]]}]


And finally a cell for watching the count.


This code appears to be stable and reliable, mainly because when the dialog appears, the background task stops and waits for dialog to be dismissed. Therefore, race conditions are avoided.

  • $\begingroup$ Good answer. Although not revealed in the question but in the comments, I am working with multiline data not a number specifically the code looks very much like Graphics[Circle[{0, 0}], ImageSize -> {100, Automatic}] $\endgroup$
    – William
    Aug 3, 2013 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.