They mean different things. "When" = "at the time that." "While" = "during the time that."
Did you lose weight when you were running every day?
You must carry water with you when you are running long distances.
I have a chance to enjoy the scenery along the trail while I am running on the hiking path.
It is hard to carry on a conversation while you are running.