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.
This commit is contained in:
Vadim Zeitlin
2021-01-06 23:54:51 +01:00
parent d22956a56a
commit ec2ea5c7fa

View File

@@ -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;