How to abort evaluation of a sequence of inputs?

前端 未结 3 999
北海茫月
北海茫月 2020-12-10 19:57

By default pressing Alt+. or calling Abort[] within the evaluation causes abort of the currently evaluating input. But when working in the

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 20:55

    I have found a way to do what I want. The only problem is that at this moment I do not know how to check whether the FrontEnd has some pending inputs. I am forced just to wait 1 second. In the most cases it is sufficient time for sending all pending inputs to the kernel but probably not always.

    In[1]:= $new$PreRead = False;
    AbortAllPendingInputs := 
      AbortProtect[If[! $new$PreRead, $new$PreRead = True;
        $TimeOfAbort = SessionTime[];
        last$PreRead = ToString[Definition[$PreRead], InputForm];
        ClearAll[$PreRead];
        $PreRead := If[TrueQ[SessionTime[] - $TimeOfAbort < 1], "",
           $new$PreRead = False;
           ClearAll[$PreRead];
           If[last$PreRead === "Null", #, 
            ToExpression[last$PreRead]; $PreRead@#]
           ] &;]];
    
    In[3]:= f := CheckAbort[Pause[10], AbortAllPendingInputs; Abort[]]
    
    In[4]:= While[True, f]
    While[True, f]
    While[True, f]
    
    Out[4]= $Aborted
    

    But I am still looking for more elegant solution. I think that if the FrontEnd creates an input queue for MathKernel it probably can also cancel this queue.

提交回复
热议问题