Block until window closed    
   I would like to display a window, and block the program execution until the user decides to continue. This is done normally the following way:
Mat img1,img2;
//...process...
namedWindow("Image1",WINDOW_NORMAL);
imshow("Image1",img1);
waitKey();
destroyAllWindows();
namedWindow("Image2",WINDOW_NORMAL);
imshow("Image2",img2);
 The only problem with this approach is if the user instinctively closes the window using the X button. Then, as no HighGUI windows are active, waitKey() will never return and the program blocks.
Is there any solution, so the program waits a keypress OR the window to be closed?
 
 
hi @kbarni. what is your OpenCV version and OS?. in Windows with latest OpenCV here your code works flawlessly.
it seems @berakhas solved the problem on windows.
I'm using Ubuntu and OpenCV 4.3. I have to check on windows...