X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5cd3213b65b195186b5ec65a3ae73bb0dc7ec19a..d8eff331e23435d9d8d6483a40f6fd9997a13f87:/samples/except/except.cpp diff --git a/samples/except/except.cpp b/samples/except/except.cpp index db66fb76de..7ea7b7b159 100644 --- a/samples/except/except.cpp +++ b/samples/except/except.cpp @@ -44,6 +44,8 @@ #include "wx/utils.h" #include "wx/msgdlg.h" #include "wx/icon.h" + + #include "wx/thread.h" #endif // ---------------------------------------------------------------------------- @@ -128,10 +130,13 @@ protected: // catch exceptions which occur in MyFrame methods here virtual bool ProcessEvent(wxEvent& event); -#ifdef __WXDEBUG__ - // show how an assert failure message box looks like + // provoke assert in main or worker thread + // + // this is used to show how an assert failure message box looks like void OnShowAssert(wxCommandEvent& event); -#endif // __WXDEBUG__ +#if wxUSE_THREADS + void OnShowAssertInThread(wxCommandEvent& event); +#endif // wxUSE_THREADS private: // any class wishing to process wxWidgets events must use this macro @@ -186,9 +191,10 @@ enum #if wxUSE_ON_FATAL_EXCEPTION Except_HandleCrash, #endif // wxUSE_ON_FATAL_EXCEPTION -#ifdef __WXDEBUG__ Except_ShowAssert, -#endif // __WXDEBUG__ +#if wxUSE_THREADS + Except_ShowAssertInThread, +#endif // wxUSE_THREADS Except_Dialog, Except_Quit = wxID_EXIT, @@ -214,9 +220,10 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) #if wxUSE_ON_FATAL_EXCEPTION EVT_MENU(Except_HandleCrash, MyFrame::OnHandleCrash) #endif // wxUSE_ON_FATAL_EXCEPTION -#ifdef __WXDEBUG__ EVT_MENU(Except_ShowAssert, MyFrame::OnShowAssert) -#endif // __WXDEBUG__ +#if wxUSE_THREADS + EVT_MENU(Except_ShowAssertInThread, MyFrame::OnShowAssertInThread) +#endif // wxUSE_THREADS END_EVENT_TABLE() BEGIN_EVENT_TABLE(MyDialog, wxDialog) @@ -307,14 +314,17 @@ void MyApp::OnAssertFailure(const wxChar *file, const wxChar *cond, const wxChar *msg) { - if ( wxMessageBox - ( - wxString::Format("An assert failed in %s().", func) + - "\n" - "Do you want to call the default assert handler?", - "wxExcept Sample", - wxYES_NO | wxICON_QUESTION - ) == wxYES ) + // take care to not show the message box from a worker thread, this doesn't + // work as it doesn't have any event loop + if ( !wxIsMainThread() || + wxMessageBox + ( + wxString::Format("An assert failed in %s().", func) + + "\n" + "Do you want to call the default assert handler?", + "wxExcept Sample", + wxYES_NO | wxICON_QUESTION + ) == wxYES ) { wxApp::OnAssertFailure(file, line, func, cond, msg); } @@ -348,10 +358,12 @@ MyFrame::MyFrame() menuFile->AppendCheckItem(Except_HandleCrash, wxT("&Handle crashes\tCtrl-H")); menuFile->AppendSeparator(); #endif // wxUSE_ON_FATAL_EXCEPTION -#ifdef __WXDEBUG__ menuFile->Append(Except_ShowAssert, wxT("Provoke &assert failure\tCtrl-A")); +#if wxUSE_THREADS + menuFile->Append(Except_ShowAssertInThread, + wxT("Assert failure in &thread\tShift-Ctrl-A")); +#endif // wxUSE_THREADS menuFile->AppendSeparator(); -#endif // __WXDEBUG__ menuFile->Append(Except_Quit, wxT("E&xit\tCtrl-Q"), wxT("Quit this program")); wxMenu *helpMenu = new wxMenu; @@ -444,8 +456,6 @@ void MyFrame::OnHandleCrash(wxCommandEvent& event) #endif // wxUSE_ON_FATAL_EXCEPTION -#ifdef __WXDEBUG__ - void MyFrame::OnShowAssert(wxCommandEvent& WXUNUSED(event)) { // provoke an assert from wxArrayString @@ -453,7 +463,34 @@ void MyFrame::OnShowAssert(wxCommandEvent& WXUNUSED(event)) arr[0]; } -#endif // __WXDEBUG__ +#if wxUSE_THREADS + +void MyFrame::OnShowAssertInThread(wxCommandEvent& WXUNUSED(event)) +{ + class AssertThread : public wxThread + { + public: + AssertThread() + : wxThread(wxTHREAD_JOINABLE) + { + } + + protected: + virtual void *Entry() + { + wxFAIL_MSG("Test assert in another thread."); + + return 0; + } + }; + + AssertThread thread; + thread.Create(); + thread.Run(); + thread.Wait(); +} + +#endif // wxUSE_THREADS void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) {