Both when and while are used for action; however, there is quite a bit difference between them.
When = To introduce a single completed event that happens in the middle of a longer event:
When the gardener was playing piano, Nancy plucked the flowers.
She threw empty bottles on the street When I passed by.
Who will babysit Hilda when Martha leaves?
What did you do when somebody stole the money from your wallet?
Martha always listens to children except when she is getting off.
While = When we have two continuous actions, irrespective of them being in the present or in the past, we use while:
Nancy plucked the flowers while the gardener was playing piano.
She threw empty bottles on the sidewalk while I was passing by.
You should brush up on your English while she’s in the town.
He was seeing another woman while the wife was away.
I heard somebody climb through the window into the kitchen while you were fast asleep.
When there is a single action before a continuous action, any one of these two is OK:
The lights went out when Mrs Joseph was ironing the clothes. √
The lights went out while Mrs Joseph was ironing the clothes. √
However, when a continuous action is followed by a single action, when is used:
Mrs Joseph was ironing the clothes when the lights went out. √
Mrs Joseph was ironing the clothes while the lights went out. ✗