+1 Very nice. I hope his main evaluations aren't triggered by buttons with Method->"Queued". Any way to cover those cases too?
–
RojoFeb 9 '12 at 17:58

@Rojo If buttons are generated in the same session, then I'd write a preprocessor that would dynamically generate code where the body of the button is wrapped in withMainEvaluation. If not, one can probably write code which would pre-process a notebook and change that at the box level. Other than that, I don't know how to do this - this may need some lower-level stuff.
–
Leonid ShifrinFeb 9 '12 at 18:11

I'm very late with this, but isn't that question superfluous: With Method->"Queued", the evaluation is queued to be evaluated using the main link, so there by definition isn't a (-nother) main evaluation running, and mainEvaluationOngoing always returns False, which is correct when interpreted as "is another main evaluation ongoing". Or am I missing something?
–
Albert ReteyMay 22 '13 at 14:05

@AlbertRetey Yes, but IIRC, the question was about how to determine whether or not the main evaluation is ongoing, from the preemptive evaluation, not from the queued one.
–
Leonid ShifrinMay 22 '13 at 14:23

However the -7 here is an empirical factor which removes all those entries generated from executing the button code itself; it changes depending on the exact form of the code the Stack[] is embedded in, and might even depend on the Mathematica version (I don't think the inner mechanics of Button is documented, let alone guaranteed never to change).

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.