Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to use Get to load some pretty substantial packages from a custom menu in the Mathematica toolbar (added via MenuSetup.tr).
The problem is, the standard 5-second evaluation timeout seems to apply to commands executed with KernelExecute, so only a fraction of my Get is evaluated before the command times out.

I'm wondering whether there's an option that can be passed to KernelExecute (or to Item / MenuItem) that will remove that time constraint so that my command can be executed completely.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

As of version 9, MenuItem now takes the Method option, similarly to Dynamic and Button. Which means that you can make it a "Queued" evaluation. To demonstrate:

FrontEndExecute[
 FrontEnd`AddMenuCommands[
  "Open", {MenuItem["Delayed New", 
    System`KernelExecute[Pause[10]; NotebookCreate[]], 
    System`MenuEvaluator -> Automatic]}]]

Here we have a command which adds a new menu item after the "Open" menu item which waits 10 seconds, then opens a notebook. And it fails. Similar to what you're seeing. So, let's reset the menus:

FrontEndExecute[FrontEnd`ResetMenusPacket[{Automatic}]]

and add it again, but this time with the Method option:

FrontEndExecute[
 FrontEnd`AddMenuCommands[
  "Open", {MenuItem["Delayed New", 
    System`KernelExecute[Pause[10]; NotebookCreate[]], 
    System`MenuEvaluator -> Automatic, Method -> "Queued"]}]]

This has all the same features and limitations of the "Queued" usage elsewhere. For example, if you have a bunch of Shift+Enter evaluations in your queue, this goes to the back of the line and waits for those.

share|improve this answer
    
Fantastic! This allows my menu to function precisely as intended. It's also worth noting that Method->"Queued" also seems to allow kernel-blocking dialogs such as InputDialog to run from within MenuItem without hanging the frontend. –  bdot Nov 2 '13 at 20:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.