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:
@@ -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;
|
||||
|
Reference in New Issue
Block a user