12
$\begingroup$

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?

$\endgroup$
3
  • 1
    $\begingroup$ I rephrased the question and made the example self contained. Feel free to revert my changes. p.s. +1, interesting. $\endgroup$
    – Kuba
    Jan 18, 2019 at 10:22
  • $\begingroup$ @Kuba Thanks! Your rephrase seems great! $\endgroup$
    – Veteran
    Jan 18, 2019 at 10:25
  • 2
    $\begingroup$ WRI says it is a 'known issue' so I guess bugs applies. $\endgroup$
    – Kuba
    Jan 18, 2019 at 15:13

2 Answers 2

4
$\begingroup$

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
$\endgroup$
1
  • $\begingroup$ Thanks for this strange but functioning workaround! $\endgroup$
    – Luca
    Mar 29, 2023 at 18:26
2
$\begingroup$

I found this clunky workaround:

file = NotebookOpen[filename];
FrontEndExecute[FrontEndToken[file, "SelectAll"]]
FrontEndExecute[FrontEndToken[file, "Evaluate"]]
$\endgroup$
1
  • 1
    $\begingroup$ 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) $\endgroup$
    – Lukas Lang
    May 10, 2019 at 8:01

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.