I think the normal thing to be done in this case is running your do/while loop in a separate thread so you can handle all the messages, including toolbar notifications, from the main application thread normally. Your while loop condition should check on a global variable that is to be changed by the toolbar message to fail the condition.