They are both grammatically correct. The action you believe is the foreground action should be said first - in the main clause. The action you believe is the background action - the on-going action taking place possibly before and/or after the foreground action - should be placed in the "while" clause.
New words, one handy idiom, and a 2-minute quiz — delivered to your inbox to keep your streak alive.