From 45b480c6ddf38b8f7255a95c56fddcd52bfa33d2 Mon Sep 17 00:00:00 2001 From: Ron Hale-Evans Date: Fri, 24 Jun 2016 10:25:47 -0700 Subject: [PATCH] Rewrote in simple language. Deleted an irrelevant example. --- docs/reference/control/in-background.md | 35 ++++++++++++------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/docs/reference/control/in-background.md b/docs/reference/control/in-background.md index 24aa8a9b..0e0de180 100644 --- a/docs/reference/control/in-background.md +++ b/docs/reference/control/in-background.md @@ -1,15 +1,25 @@ -# In Background +# Run In Background -Run code in the background as a separate process or thread; for more information on this advanced construct, see [the micro:bit - a reactive system](/device/reactive). +Run part of a program while the rest of it is doing something else. ```sig control.inBackground(() => { }) ``` +### ~hint + +For more information, read +[The micro:bit - a reactive system](/device/reactive). +It is pretty advanced! + +### ~ + ### Example -The example below shows how a background process can be used to display the current value of the global variable `num`, while code (like the `on button pressed` handler) can change the value of the variable. +This program shows how running in the background can say what is +stored in a variable like `num`, while another part (``on button pressed``) +changes what is stored there. ```blocks let num = 0 @@ -24,7 +34,8 @@ input.onButtonPressed(Button.A, () => { }) ``` -The code below using the `forever` loop is equivalent to the code above +This program does the same thing, but in a more usual way, +with a ``forever`` loop. ```blocks let num = 0 @@ -36,20 +47,8 @@ input.onButtonPressed(Button.A, () => { }) ``` -### Contention for the LED display - -If you have multiple processes that each show something on the LED screen, you may get unexpected results. Try, for example: - -```blocks -basic.forever(() => { - basic.showNumber(6789, 150) -}) -input.onButtonPressed(Button.A, () => { - basic.showNumber(2, 150) -}) -``` - ### See also -[while](/blocks/loops/while), [forever](/reference/basic/forever), [on button pressed](/reference/input/on-button-pressed) +[while](/blocks/loops/while), [forever](/reference/basic/forever), +[on button pressed](/reference/input/on-button-pressed)