Forever runs before OnStart finishes


My Forever loop starts before the OnStart has finished

I get runtime error 40 or 42, (NULL reference)


Under the covers, the micro:bit will start every Forever loop in it’s own fiber (a fiber is a light-weight thread that can run independently from other fibers – a bit like a separate block of code in Scratch, for example). The On Start block will run to completion, then Forever blocks will start executing.

However, if there are any pause statements (or function usage that has an inherent pause facility) inside On Start, this will cause the fiber scheduler to start all other fibers, and your Forever loops will start before the On Start has completed.

This is normal operation.


Remove any statements in your On Start that pause.

Alternatively, if you need pauses in your On Start for some reason, at the end of your On Start set a boolean flag to True, and check the value of that flag in your Forever loops – only starting the Forever loop content once that boolean flag has been set to True.


fibers: https://lancaster-university.github.io/microbit-docs/concepts/#concurrency


MS (https://github.com/Microsoft/pxt/issues/1324)

0 found this helpful
All for Joomla All for Webmasters