This question gets asked like a bijillion times a month. Did you try a search? You need a loop that sits there waiting for the user to press the button. (Note: the only thing that should be in the loop is the actual operation. One-time configurations should be outside the loop. Same goes for closing the resources.) A simple state machine is an ideal solution. I'd recommend reading this article: Application Design Patterns: State Machines