How to pause calculation waiting for the variable return from Dialog box

I have a long script, in the middle of it I have this dialog window:

CreateWindow[
  DialogNotebook[{TextCell["Where to insert new rows? "], 
    InputField[Dynamic[nm], String], 
    Row[{DefaultButton[DialogReturn[kk = nm]], CancelButton[]}]}]];

How can I pause and wait till one enters the value nm and then after it proceed with updated variable kk which is being used further forward? Thanks.

Use DialogInput instead of CreateWindow.

Thanks.

But even with this it doesn’t seem to pause and wait. In the code below, it should bring a matrix of InputFields and then when you increase n (main entry variable), it should Insert a row in the matrix, but it doesn’t:

Clear[f, kk];
T = {}; kk = Null;
InputField[Dynamic[n,
  {n = #;
    
    AppendTo[T, n];
    
    If[Length[T] == 1, A = Array[c, 3 n],
     
     If[Length[T] >= 2,   
      
      DialogInput[
       DialogNotebook[{TextCell["Where to insert new rows? "], 
         InputField[Dynamic[nm], String], 
         Row[{DefaultButton[DialogReturn[kk = nm]], 
           CancelButton[]}]}]];
      
      A = 
       Flatten@Insert[A, Table[c, {i, 3 ( T[[-1]] - T[[-2]])}], 
         3 kk]; Print[kk];
      
      ]
     ];
    Print[kk];
    Print@
     Grid[Table[{"Amp", i, " = ", 
        With[{i = i}, 
         InputField[Dynamic[A[[3 i - 2]]], FieldSize -> Tiny]],
        
        "\[Mu]", i, " = ", 
        With[{i = i}, 
         InputField[Dynamic[A[[3 i - 1]]], FieldSize -> Tiny]],
        
        "\[Sigma]", i, " = ", 
        With[{i = i}, InputField[Dynamic[A[[3 i]]], FieldSize -> Tiny]]
        
        }, {i, n}]];
    
    f = Sum[
      A[[3 i - 2]] *Sqrt[2 \[Pi]]
        A[[3 i]] PDF[NormalDistribution[A[[3 i - 1]], A[[3 i]]], 
        t], {i, n}];
    
    
    Print@
     Show[ListPlot[gr3, Joined -> True], 
      Plot[f, {t, -1.8, -1.10}, PlotStyle -> Red]]
    
    
    } &]]

Not sure I understand why.

The problem is that Dynamic uses preemptive link, which is not queued. It does what it can as soon as it can. Take a look at Advanced Dynamic Functionality tutorial.

Currently only Button and ActionMenu is able to execute on main link, with Method->"Queued".

You could try to walk-around that with SynchronousUpdating->False which will send evaluation from Dynamic object via main link. It does not really work with Dynamic inside controllers like InputField.

I see the problem now. thanks.

Why is it when I put semicolon after Dynamics it’s not being evaluated?

If you are interested in Dynamic and making user interfaces with Mathematica then:

  • Introdution to Dynamic
  • Advanced Dynamic Functionality

tutorials are a must. I’m higly encouraging you to go through them before you proceed, it will save your time in the future.

Your problem is described in the former one. This is a fundamental property of Dynamic, it matters only when displayed.