From ec2ea5c7fa7cde5c6e1e56d601514a1ec3fb0049 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 6 Jan 2021 23:54:51 +0100 Subject: [PATCH] Handle request still in progress gracefully on shut down Cancel the request and wait until it actually is cancelled when exiting the sample. This is a bit ugly, especially the busy-waiting part, but still better than potentially crashing. --- samples/webrequest/webrequest.cpp | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) 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;