diff --git a/samples/webrequest/webrequest.cpp b/samples/webrequest/webrequest.cpp index 51d40eed6c..5b0bb505e2 100644 --- a/samples/webrequest/webrequest.cpp +++ b/samples/webrequest/webrequest.cpp @@ -45,6 +45,8 @@ public: // set the frame icon SetIcon(wxICON(sample)); + Bind(wxEVT_CLOSE_WINDOW, &WebRequestFrame::OnClose, this); + // Prepare UI controls wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL); @@ -174,6 +176,18 @@ public: &WebRequestFrame::OnProgressTimer, this); } + virtual ~WebRequestFrame() + { + // We have to block until the web request completes, but we need to + // process events while doing it. + Hide(); + + while ( m_currentRequest.IsOk() ) + { + wxYield(); + } + } + void OnStartButton(wxCommandEvent& WXUNUSED(evt)) { wxLogStatus(this, "Started request..."); @@ -376,6 +390,35 @@ public: m_urlTextCtrl->SetValue(defaultURL); } + void OnClose(wxCloseEvent& event) + { + if ( m_currentRequest.IsOk() ) + { + if ( event.CanVeto() ) + { + wxMessageDialog dialog + ( + this, + "A web request is in progress, " + "closing the window will cancel it.", + "Please confirm", + wxYES_NO + ); + dialog.SetYesNoLabels("Cancel and close", "Don't close"); + + if ( dialog.ShowModal() != wxID_YES ) + { + event.Veto(); + return; + } + } + + m_currentRequest.Cancel(); + } + + event.Skip(); + } + private: wxNotebook* m_notebook; wxTextCtrl* m_urlTextCtrl;