Both when and while can be used to talk about actions or situations that take place at the same time.
Swan identifies the following differences:
We can use both words to introduce a longer 'background' action or situation, which is/was going on when something else happens/happened.
Somebody broke into the house when they were playing cards.Note that when and while clauses can go at the beginning or end of sentences.
While they were playing cards, somebody broke into the house.
2 Simultaneous long actions
We usually use while to say that two longer actions or situations go / went on at the same time.
While you were reading the paper, I was working.If we are talking about ages and periods of life, we use when:
When I was a child we lived in London (NOT While I was a child …)
His parents died when he was twelve (NOT … while he was twelve)
3 Simultaneous short actions
We can use (just) when to say that two short actions or events happen / happened at the same time:
I thought of it (just) when you opened your mouth.While is not possible in this situation.
4 Reduced clauses
It is often possible to leave out subject + be after when and while:
While/When in Germany, he got to know a family of musicians. (=While/When he was in Germany …)Practical English Usage, Michael Swan, OUP, pp. 73-74
While vs whilst
There is no difference in meaning between these two words. In British English whilst is considered to be a more formal and literary word than while. The different spellings that exist today have their origins in changes to the words in Middle English and later.
See World Wide Words: While versus whilst for an explanation.
For a quiz about when and while see the following website:
Learning English | BBC World Service
For many examples of the use of when and while, see the Web Concordancer: