How does MessageBox stop the execution of the code until the user clicks the button? Something like _getch() ?

    1 answer 1

    MessageBox is a blocking function. Therefore, it cannot use the message loop inside itself (since the message loop hangs waiting for the MessageBox finish).

    Therefore, a new nested message loop is started inside the MessageBox for the duration of the function. Keyboard and mouse events are handled in this new message loop, so the dialog box does not hang. At the end of the work, the new cycle is completed, and the processing of the old one is resumed from the point at which it was frozen by the MessageBox function.


    This, by the way, means one not very pleasant feature: if your MessageBox shown as part of processing one of the window messages (perhaps indirectly), then during its operation the same window message may come again and your code will be called recursively. Therefore, care must be taken to either block such nested processing, or leave the data structures in a normal, consistent state.