[#72056232 #71979506 finish] Code refactoring to stop using infinity loop while waiting for a input state.