2016-03-26 00:47:20 +01:00
# In Background
2016-04-13 17:27:45 +02:00
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 ).
2016-03-26 00:47:20 +01:00
2016-03-26 05:13:09 +01:00
```sig
2016-03-26 00:47:20 +01:00
control.inBackground(() => {
})
```
### 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.
2016-03-26 05:13:09 +01:00
```blocks
2016-03-26 00:47:20 +01:00
let num = 0
control.inBackground(() => {
while (true) {
basic.showNumber(num, 150)
basic.pause(100)
}
})
2016-03-30 06:17:57 +02:00
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
num++;
})
```
The code below using the `forever` loop is equivalent to the code above
2016-03-26 05:13:09 +01:00
```blocks
2016-03-26 00:47:20 +01:00
let num = 0
basic.forever(() => {
basic.showNumber(num, 150)
})
2016-03-30 06:17:57 +02:00
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
num++;
})
```
### 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:
2016-03-26 05:13:09 +01:00
```blocks
2016-03-26 00:47:20 +01:00
basic.forever(() => {
basic.showNumber(6789, 150)
})
2016-03-30 06:17:57 +02:00
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
basic.showNumber(2, 150)
})
```
### See also
2016-06-14 23:20:45 +02:00
[while ](/blocks/loops/while ), [forever ](/reference/basic/forever ), [on button pressed ](/reference/input/on-button-pressed )
2016-03-26 00:47:20 +01:00