# Synchronizing code in Dynamics with other procedures

I have huge code and some problem. I don't know how to make example. I'm afraid I can't make good SMALL example to demonstrate you the problem. So I'll try to explain. Maybe somebody know about such problem.

I have some DynamicModule. Large grid of plots, controls and tables of expressions some of them Dynamic.

Dynamic[myplots=Grid[{{Plot[...], Plot[...]}}], TrackedSymbols:>{expr}]


I have some function that execute some calculations what change Dynamic plots and Tables.

What's the problem:

expr=values;
Export["file.png", myplots];


When I'm trying to export calculated data and dynamically updating data from grid I get empty grid.

Changing expr launch calculating Dynamic grids. And Export saves file until calculations complete. So I get empty file.

expr=values;
Pause[5];
Export["file.png", myplots];


All calculations complete and I have right result in "file.png".

But I never know how much time I need. It depends on input data size. Sometimes it 1 sec, sometimes it 30sec. So always have to wait 30sec to guarantee right result it's a bad idea.

I look at some commands as WaitAll:

expr=values;
WaitAll[myplots];
Export["file.png", myplots];


Still nothing. I just don't understand.

What did I do wrong and how can I fix it?

• Does FinishDynamic[] help? – Simon Woods Jul 15 '16 at 17:45
• YES! Thx. Solved. I'm so newbie in WM... I tried so much, but solution always was so simple. Thx – Vasily Jul 15 '16 at 17:53
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Jul 15 '16 at 18:03
• Another usage tip: To response to a comment, use @ + username, such as @SimonWoods, to ensure they are notified of your response. – Michael E2 Jul 15 '16 at 18:04

I think you should change your setup a little bit.

Including procedures inside Dynamics is useful somethimes, for better events handling, but nothing fancy is going on here. Thus it is better schedule calculations in a more stable way, by moving the Grid generation outside Dynamic.

Your exporting procedure clearly expects myplots to have an up to date value with respect to expr.

expr=values;
Export["file.png", myplots];


So keeping myplots=Grid[...] in a Dynamic is not a stable solution.

Why it is better to keep "procedures" outside and use Dynamics only for displaying things?

• You can force FinishDynamic[] before the Export but if your Dynamic[myplots...] is not currently visible on a screen it won't be updated and assigned anyway.

• ref: "FinishDynamic[] does not return until all Dynamic objects are updated and displayed. " - "all" sounds like an overkill for our purpose, doesn't it?

• Dynamic[ myplots=Grid[...] ] requests this Grid calculation over the PreemptiveLink, it will timeout after DynamicEvaluationTimeout (6 seconds by default) and it will freeze other FrontEnd activities till it is finished.

Which isn't critical since you can icrease the timeout limit or use SynchronousUpdating -> False (runs on the MainLink), but why complicate things.

If something is not clear I'm encouraging you to lookup those symbols in Help and read Advanced Dynamic Functionality tutorial (and related)

What I'd change is the following:

• create a plot updating procedure (for flexibility and readability)

updatePlot[]:= (myplots=Grid[...]);


You can put it in DynamicModule's Initialization.

• insert it in the exporting procedure

expr=values;
updatePlot[];
Export["file.png", myplots];

• change your Dynamic content:

Dynamic[expr;myplots, TrackedSymbols:>{expr}]


Notice expr;, even though it is mentioned in TrackedSymbols it has to be explicitly stated in Dynamic's body to be properly tracked.

• additionaly you can introduce progress state variable to show a ProgressIndicator instead of myPlots before they are recalculated. Analogously to Palette button with progress bar, which is nice from user perspective.

• I added FinishDynamic[] and all works. BUT when I added SynchronousUpdating -> False into my plot grids, FinishDynamic[] don't work anymore. Why? And as I say I have some controls inside DynamaicModules with Grid which generates plots. So I don't know how can I make separate procedure updatePlot[] as u recommended. And @Kuba don't work. – Vasily Jul 17 '16 at 19:22
• 5 min left. I have to say that I have large grid of DynamicModules with plots, tables and controls, control variables are local, so if I make it global it slowdown PC. And because of it, I don't know how can I make separate procedure updatePlot[] as u recommended and don't have troubles with performance. Or because I'm newbie, I don't know how can I make procedure, that will use local variables from other DynamicModules which display and control myplots. Thx. And @Kuba don't work. – Vasily Jul 17 '16 at 19:35
• @Vasily you don't have to pin the name of the author of the post you are commenting. I will try to answer your tomorrow, I really have to go now. Shortyly, DynamicModule[{...}, Dynamic[...;myplot,...], Initialization:>(updatePlot[]:=(myplot...))]` hard to say more without more precise description of your setup. Ok. have to go. – Kuba Jul 17 '16 at 19:39
• @Vasily this is why FinishDynamic[] is not working iwth SU->False: mathematica.stackexchange.com/q/120942/5478 but I don't have any official sources yet. – Kuba Jul 17 '16 at 22:58
• So there is no solution for my problem? – Vasily Jul 17 '16 at 23:09