while( .... ) { # some non-blocking stuff if( Tk::MainWindow->Count ) { local $inMainLoop = 1; DoOneEvent(0); } # if else { # no more mainwindows, finish somehow } # else # other non-blocking stuff } # while