Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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];
StartScheduledTask[task];
Print@Button["RemoveScheduleTask", RemoveScheduledTask[task]];
InputField[Dynamic[a], Number]
Dynamic[a]

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?

share|improve this question
    
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. –  Michael E2 Jul 27 '13 at 19:01
    
@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. –  Liam William Jul 27 '13 at 19:23
    
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. –  Michael E2 Jul 27 '13 at 21:05
    
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. –  m_goldberg Jul 28 '13 at 0:56
    
@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 –  Liam William Jul 28 '13 at 1:27

1 Answer 1

up vote 1 down vote accepted

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},
      Column[{
        Row[{Style["Enter an integer", "Helvetica", 12]}],
        InputField[Dynamic@val, Number],
        Button["OK", DialogReturn[Round@val]]
      }]
    ]
  )

dialog.png

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]];
Column[{
  Button["Update count", update[], Method -> "Queued"],
  Button["Remove Task", RemoveScheduledTask[First@ScheduledTasks[]]]}]

buttons.png

And finally a cell for watching the count.

Dynamic[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.

share|improve this answer
    
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}] –  Liam William Aug 3 '13 at 21:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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