Use a State Machine: Good paper on Event-Driven Programming - #6 by awneil
You certainly do not want a while(1) loop