You use 'while' when you want to focus attention on what was happening ALL THE TIME during a period of time. For example, if it were from 9am to 10am, you use 'while' to focus attention on what was happening at 9:05, 9:10, 9:15 ... 9:55,10:00.
Eg, "While I was driving to work I listened to the radio." I want to tell you that all the time, almost every minute during that entire period, I listened to the radio.
In contrast, 'when' is not as specific. You want to focus on a period of time, but NOT necessarily what was happening minute by minute during that period of time.
Using the same example, but 'when', "When I was driving to work, I listened to the radio'". Here I want to focus your attention on the period I was driving to work, BUT only generally. I am not telling you what happened constantly, minute by minute during that period I drove to work. I am only telling you that I was listening to the radio in a more general sense as I went to work. I am not trying to tell you it happened all the time I was driving as with 'while'.