12

The following procedure can be easily aborted using Evaluation > Abort Evaluation menu item:

Do[j + k, {j, 1, 10000}, {k, 1, 10000}]

But it is not possible if NotebookEvaluate was used:

nb = CreateDocument[ ExpressionCell[Defer[Plot[Sin[x], {x, 0, 2 Pi}]], "Input"]]
NotebookEvaluate[nb, InsertResults -> True];

Now run this again:

Do[j+k,{j,1,10000},{k,1,10000}]

and try to abort. It does not work and the loop is on.

Anyone knows why? Is there any alternative to NotebookEvaluate?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Veteran
  • 439
  • 2
  • 7

2 Answers2

4

I was able to find this workaround, I know not what it does.

In[1]:= Pause[3];(*abortable*)

Out[1]= $Aborted

In[2]:= Quiet@NotebookEvaluate@"abc";

In[3]:= Pause[3];(unabortable)

In[4]:= MathLink`LinkAddInterruptMessageHandler[$ParentLink]

In[5]:= Pause[3];(Aborts are back!)

Out[5]= $Aborted

Jason B.
  • 68,381
  • 3
  • 139
  • 286
2

I found this clunky workaround:

file = NotebookOpen[filename];
FrontEndExecute[FrontEndToken[file, "SelectAll"]]
FrontEndExecute[FrontEndToken[file, "Evaluate"]]
  • 1
    It should be noted that this workaround behaves very differently to NotebookEvaluate in a few aspects: NotebookEvaluate evaluates the notebook and waits until it is done, while your method simply enqueues it for evaluation. (Also, the line numbers are different) – Lukas Lang May 10 '19 at 08:01